LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2011, 09:22 AM   #31
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190

Quote:
Originally Posted by Sergei Steshenko View Post
Modern compilers are pretty smart regarding optimization.
I was about to say: Not in my experience.

But reading on, I see the correct response is: We don't agree on the definition of "smart".

Quote:
I've noticed that compiler eliminates about 1/3 (!) of unneeded assignments.
I understand both the fact that this can be a challenging and impressive accomplishment for an optimizer and the fact that it is not good enough for a programmer who cares about the size or speed of the generated code.

Quote:
My point is that most likely if one has

Code:
Foo *ptr = NULL; // ptr is of class 'auto'
// ptr is not actually used until next line:
ptr = true_value;
My point was that I wouldn't code that and programmers working for me shouldn't code that.

Wherever practical, delay the declaration of a local variable until the point that it is first initialized.

I don't suggest that to make life easier for the optimizer (though it sometimes does). I suggest that to make life easier for the maintenance programmer. Both the compiler and the maintenance programmer need to understand the scope of local variables. If the original programmer makes the scope as short and obvious as possible, that makes things a little easier for the optimizer and a lot easier for the maintenance programmer.

So I assume the easy cases where redundant pre initializing with zero can be eliminated by the optimizer have already been cleanly eliminated by the programmer.

That leaves the difficult cases: The programmer cannot cleanly delay declaration of the local until the first assignment. The programmer believes that all paths initialize the local even if uninitialized where declared. But that fact is not obvious (so might not be safe).

If the programmer puts in the redundant pre initialization for safety, the same reasons that make that pre initialization appropriate for safety, often make it opaque to the optimizer. So the redundant assignment will occur in the fully optimized code. (I look at a lot of compiler optimized assembler code).

So that safety vs. performance style decision doesn't have a general right/wrong. It depends on the programmer.
 
Old 04-12-2011, 09:35 AM   #32
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by Sergei Steshenko View Post
Code:
Foo *ptr = NULL;
// give a true value to 'ptr' later
- null pointers are easily seen as wrong during debugging.
Actually, in my code it's perfectly normal for pointers to be NULL (that's how it represents an empty variable in the interpreted language).
 
Old 04-12-2011, 09:58 AM   #33
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Actually, in my code it's perfectly normal for pointers to be NULL (that's how it represents an empty variable in the interpreted language).
But if/when your program crashes and you see that the crash is caused by a null pointer, you immediately know you've forgotten to initialize the offending pointer.
 
Old 04-12-2011, 10:03 AM   #34
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
...
My point was that I wouldn't code that and programmers working for me shouldn't code that.

Wherever practical, delay the declaration of a local variable until the point that it is first initialized.
...
And my point is: "Don't code in plain C/C++ regarding pointers, always use the proposed macro".

I.e. even if you code

Code:
Foo *ptr = TRUE_PTR_VALUE;
always write it using

Code:
INTRODUCE_POINTER(Foo, ptr); ptr = TRUE_PTR_VALUE;
.

With such an approach, regardless of (im)possibility to initialize a pointer at the place of declaration the pointer is always initialized - at least with NULL.
 
Old 04-12-2011, 11:37 AM   #35
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
My original question is solved, so I'm marking the thread as solved. But you can still discuss what you were talking about.

Anyway, after fixing a problem with filling in function parameters, it's starting to work:

Code:
new BlockNode (
		new AssignmentNode(NULL, "i", new IntegerNode(10)),
		new LoopNode (
			new CallNode(new MemberNode(new MemberNode(NULL, "i"), "__comp_ne__"), (new NodeCallParamList())->add(new IntegerNode(-1))),
			new BlockNode (
				new CallNode(new MemberNode(new MemberNode(NULL, "i"), "print"), new NodeCallParamList()),
				new AssignmentNode(NULL, "i", new CallNode(new MemberNode(new MemberNode(NULL, "i"), "__op_minus__"), (new NodeCallParamList())->add(new IntegerNode(1))))
			)
		)
	);
Code:
$ ./lang
10
9
8
7
6
5
4
3
2
1
0
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache2 segfaulting Cheater512 Linux - Networking 1 07-12-2006 11:05 AM
slackware 9.0 everything segfaulting ranger12002 Slackware 6 10-31-2003 06:47 PM
RPM segfaulting wapcaplet Linux - Software 10 10-22-2003 04:45 PM
segfaulting on end acid_kewpie Programming 11 10-11-2002 04:53 PM
Gcc segfaulting salman Linux - General 1 07-14-2002 07:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration