In C++, when a function is loaded are ALL of its statically allocated variables placed on the stack immediately, or are local variables put on the stack only when they are in scope?
In C++, when a function is loaded are ALL of its statically allocated variables placed on the stack immediately, or are local variables put on the stack only when they are in scope?
The compiler calculates the size of the function's variables and upon entry the function adjusts the stack pointer by that much. At this point nothing is initialised though, it only contains garbage data.
Then upon entry to each scope block (curly braces), variables inside that scope block are initialised (their constructor is called if one was provided).
At the end of the scope block, those variables are destructed (their destructor is called if one was provided), but the stack space for them tends to exist until the end of the function.
It's also possible for variables of non-overlapping scope to re-use the same portion of the stack.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
I don't think the standard actually says anything about how/when the space is allocated on the stack for local variables [or even that they have to be on the stack, as such - but some sort of stack-like construction is obviously necessary].
In practice, what iMalc says holds true, but there is nothing saying the compiler is not ALLOWED to allocate more space on the stack in the middle of a function. It's just that the compiler would have to issue extra instructions to achieve this, and it's just making things more complicated than calculating the overall space needed.
Of course, variables do not HAVE to reside on the stack at all. Local variables may well be stored entirely in registers.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Or even if there is a stack. Yes, something stack-like is necessary, but a limited implementation could actually get by with something different.I don't think the standard actually says anything about how/when the space is allocated on the stack for local variables [or even that they have to be on the stack, as such - but some sort of stack-like construction is obviously necessary].
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Yup 'sall true!
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"