I remember hearing that lining up all scope members in the beginning of the block was no longer recommended practice with both C and C++, but does this mean it obstructs the compiler in some way when generating code, or simply that it is no longer necessary to do it, because it is not always convenient? A.K.A is it more efficient for the compiler to generate optimal allocation if locals are declared on demand?
-
1In C++ it may be inefficient to call constructors for variables that are conditionally unused. In C I doubt it makes much practical difference since the compiler can tell whether or not addresses are taken of variables, and what parts of the function use what variables. The register allocator / stack layout optimization can re-use slots where possible. However, you might encounter a holy war as to whether or not it aids readability to define locals in particular places. – Steve Jessop Dec 18 '13 at 23:39
-
3I disagree that this question is "primarily opinion-based". He could have asked "Where should I declare my locals?", which would be opinion-based, but instead he's asking "How is the compilation affected by where I declare my locals?" This is a matter which can be definitively answered. – Magnus Hoff Dec 19 '13 at 09:03
1 Answers
It used to be mandatory to declare locals up front in C, presumably because it is easier to implement a compiler when that is the case.
These days, the compiler is actually "sufficiently advanced" that this does not make any difference for POD types. Because of this, the question boils down to readability and matters of taste in C.
In C++, however, declaring of locals imply things about the execution of the relevant constructor and destructor and it can have bearing on whether or not the compiler will be able to employ certain optimizations. This means that declaring them all at the top is not equivalent to declaring them on demand, rendering your question mostly invalid for C++.
In C++, declare the variables when it is semantically correct for the program you are trying to write. (And on top of that there are also matters of readability and taste)

- 21,529
- 9
- 63
- 82
-
1The weird thing in C89 was that you could still have arbitrary expressions mixed in with the object definitions, in their initializers. So it's not as if the rule (by then) allowed compilers to do a "work out what's on the stack" step, followed by an "emit the code" step, without looking backwards. Hopefully someone who wrote a C compiler in 1985 can tell us why it was made easier by the rule :-) Further, you could define variables at the start of any block, not just functions, so it was nothing to do with the function stack frame as such. – Steve Jessop Dec 18 '13 at 23:59
-
@SteveJessop well, it isn't too hard to imagine dealing with those mixed variable/expressions in the context of the asm, with a preamble that basically just pushes space onto the stack then the next part which initializes them... basically running over that C code twice, once for allocation, once for initialization... but I am not even sure that wasn't a popular extension rather than in the language standard. – Grady Player Dec 19 '13 at 21:00