449,017 Members | 1,072 Online
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

# Style of Local Variable Declaration

 P: n/a Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? For example; void fn(struct foo *f, int bar) { struct abc d; int i; abc_init(&d); for (i = 0; i < bar; i++) { unsigned long num; struct eee *e; for (e = f->a; e < f->alim; e++) { time_t time time_now; struct tm tm_now; struct blah b; ... OR void fn(struct foo *f, int bar) { struct abc d; int i; unsigned long num; struct eee *e; time_t time time_now; struct tm tm_now; struct blah b; abc_init(&d); for (i = 0; i < bar; i++) { for (e = f->a; e < f->alim; e++) { ... I've tended to use the second form based on the belief that frequent movement of the stack pointer would lead to inefficiencies but I have no idea if my instinct is true. Mike Nov 13 '05 #1
12 Replies

 P: n/a "Michael B Allen" wrote in message news:pa**********************************@ioplex.c om... Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? If the variable's purpose fits logically within a specific block, then I would declare it there. For example; void fn(struct foo *f, int bar) { struct abc d; int i; abc_init(&d); for (i = 0; i < bar; i++) { unsigned long num; struct eee *e; for (e = f->a; e < f->alim; e++) { time_t time time_now; struct tm tm_now; struct blah b; ... OR void fn(struct foo *f, int bar) { struct abc d; int i; unsigned long num; struct eee *e; time_t time time_now; struct tm tm_now; struct blah b; abc_init(&d); for (i = 0; i < bar; i++) { for (e = f->a; e < f->alim; e++) { ... I've tended to use the second form based on the belief that frequent movement of the stack pointer would lead to inefficiencies but I have no idea if my instinct is true. Why would it imply that? Mike Nov 13 '05 #2

 P: n/a Michael B Allen wrote: Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? (snip code exemples) The first one, definitively. "Thou shall limit variables visibility to the most possible restricted block" ... I've tended to use the second form based on the belief that frequent movement of the stack pointer would lead to inefficiencies but I have no idea if my instinct is true. I'm not sure, but I guess this will greatly differ according to the implementation and the optimization options. But I would not even care about this anyway. I prefer to write clear, readable and if possible robust code. Bruno Nov 13 '05 #3

 P: n/a In message Michael B Allen wrote: Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? I've tended to use the second form based on the belief that frequent movement of the stack pointer would lead to inefficiencies but I have no idea if my instinct is true. It works both ways. By limiting the lifetime of variables you're potentially reducing stack and register usage whereever the lifetimes don't overlap. But yes, in some implementations, the stack pointer might move up and down as you move in and out of blocks. But stack pointer moves are probably cheap, and if they weren't the implementation probably wouldn't do it. And a smart compiler may well be able to figure out which variables are disjoint and allow them to share registers/memory (live range splitting), regardless of how you declare them. I really wouldn't worry about efficiency too much - just worry about the C code style. If I was worrying, I would tend to go with limited-scope declarations, not function-scope ones. -- Kevin Bracey, Principal Software Engineer Tematic Ltd Tel: +44 (0) 1223 503464 182-190 Newmarket Road Fax: +44 (0) 1223 503458 Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/ Nov 13 '05 #4

 P: n/a Michael B Allen wrote in message news:... Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? I try to limit the scope of variables as much as possible. If a variable is only used within a certain block, it should be declared within that block. [snip example] I've tended to use the second form based on the belief that frequent movement of the stack pointer would lead to inefficiencies but I have no idea if my instinct is true. Mike It probably isn't. The best way to be sure is to look at the machine code generated by your compiler. For example, I womped up a quick-n-dirty file that had one int at function scope, and one declared within a for loop. The machine code showed that space had been allocated on the stack for both at function entry; the outer scope variable was referenced by -\$4(%ebp) and the inner scope variable by -\$8(%ebp). The stack pointer doesn't change. Nov 13 '05 #5

 P: n/a In <3f***********************@news.free.fr> Bruno Desthuilliers writes: Michael B Allen wrote: Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used?(snip code exemples)The first one, definitively."Thou shall limit variables visibility to the most possible restrictedblock" Aren't you contradicting yourself? ;-) Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de Nov 13 '05 #6

 P: n/a In Michael B Allen writes: Which style of local variable declaration do you prefer; put everythingat the top of a function or only within the block in which it is used? A combination of the two. Declare one letter "temporaries" that are used all over the place at the top and the other variables only in the blocks using them. I.e. use void foo(void) { int i; volatile extern int cond1, cond2; while (cond1) { double b1var; /* code using i and b1var */ } while (cond2) { unsigned b2var; /* code using i and b2var */ } } rather than void foo(void) { volatile extern int cond1, cond2; while (cond1) { int i; double b1var; /* code using i and b1var */ } while (cond2) { int i; unsigned b2var; /* code using i and b2var */ } } The as-if rule allows the compiler to allocate *all* the locals used in a function upon function entry and to destroy them upon function return. No correct program can tell the difference. Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de Nov 13 '05 #7

 P: n/a On Wed, 05 Nov 2003 03:38:42 -0500, Michael B Allen wrote: I've tended to use the second form based on the belief that frequentmovement of the stack pointer would lead to inefficiencies but I have noidea if my instinct is true. Particular implementations are off-topic here, and you'll get more accurate answers in a group discussing your implementation. However, in most implementations and applications, I think you'll find that time spent in stack pointer movement is unmeasurable. Limiting the scope will give the compiler more optimization clues. In general, trying to out-guess a modern optimizing compiler is a losing proposition :-) -- Al Balmer Balmer Consulting re************************@att.net Nov 13 '05 #8

 P: n/a On 11/5/2003 2:38 AM, Michael B Allen wrote: Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? You've already received good advice, but one other thing to (unfortunately) consider is how your favorite debugger handles scoped variables. I've had the misfortune of coming across one that couldn't tell which block a given variable was in if 2 scopes held variables of the same name. For example, if you have: 1: void foo() { 2: if (tuesday) { 3: int val = 3; 4: printf("%d\n",val); 5: } else { 6: int val = 6; 7: printf("%d\n",val); 8: } 9: } then the printf() at line 7 would print "6" but dumping the value of "val" from the debugger at line 7 would print "3". Ed. Nov 13 '05 #9

 P: n/a Dan Pop wrote: In <3f***********************@news.free.fr> Bruno Desthuilliers writes:Michael B Allen wrote:Which style of local variable declaration do you prefer; put everythingat the top of a function or only within the block in which it is used?(snip code exemples)The first one, definitively."Thou shall limit variables visibility to the most possible restrictedblock" Aren't you contradicting yourself? ;-) woops ! Sorry ! s/first/second/, of course ! Dan Nov 13 '05 #10

 P: n/a Bruno Desthuilliers wrote: Michael B Allen wrote: Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? (snip code exemples) The first one, definitively. Sorry, went too fast. The *second* one, definitively. "Thou shall limit variables visibility to the most possible restricted block" Bruno Nov 13 '05 #11

 P: n/a John Bode wrote: Michael B Allen wrote in message Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? I try to limit the scope of variables as much as possible. If a variable is only used within a certain block, it should be declared within that block. [snip example] I tend to declare everything at the top of a function. It the urge to further block localize variables strikes I consider it an indication the function is overly complex, and should be broken into smaller functions with their own variables. This is ONLY a tendency, and the whole thing smells of style wars. However I consider the overly complex signal valuable. This also appears when there are too many local variables. -- Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net) Available for consulting/temporary embedded and systems. USE worldnet address! Nov 13 '05 #12

 P: n/a On Wed, 05 Nov 2003 12:13:24 -0500, Ed Morton wrote: On 11/5/2003 2:38 AM, Michael B Allen wrote: Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? You've already received good advice, Yes, I did. Now I have adjust my code ... Thanks to all, Mike Nov 13 '05 #13

### This discussion thread is closed

Replies have been disabled for this discussion.