[C++/C#] Local variables in loops / conditional statements

Started by
3 comments, last by Koobazaur 15 years, 11 months ago
I was wondering, how does C++ and C# (and other languages if similar) handle variables defined in a loops and conditional statements? Are they allocated on the stack when the function is called or only allocated when the loop or conditional statement is initaited? I am just wondering about the performance difference between:

int SomeVar = 12;
while(do_alot_of_times)
{
DoSomething(&SomeVar);
SomeVar = 12;
}


and

while(do_alot_of_times)
{
int SomeVar = 12;
DoSomething(&SomeVar);
}


And would it be more memory efficient to do

if(something)
{ 
int SomeVar = 12;
DoSomething(&SomeVar);
}

instead of

int SomeVar = 12;
if(something)
DoSomething(&SomeVar);

Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
Advertisement
C++ doesn't generally define these things too strictly. The object will be constructed when the scope is entered (and destructed when the scope is left), but allocation of the stack frame is implementation dependant (the allocation can be done at method entry, for example, since the size of the allocation can be known a-priori, however).

C# compiles to CIL. For CIL, the allocation happens at method entry (via the .locals directive).

As for your performance question, with your example it is trivial -- probably no difference at all in the compiled code. If the variable has complex construction and destruction semantics, then putting it in the loop is generally going to be slower since the constructor and destructor must be called (but then again, the fact that it's slower is irrelevant if this is actually the needed behavior for the scenario).
Quote:Original post by Koobazaur
Are they allocated on the stack when the function is called

Every C++ compiler I tried does that, yes.
Bear in mind that 'allocating' a local variable is not the same process as allocating something on the heap. Allocating something on the heap involves finding some free space to store it in, but if it's a local variable, it's always stored on the stack - so all the compiler has to do is remember that X bytes on the stack have been reserved for that variable. It doesn't have to do anything to set that variable up, it just earmarks the space at compile time. By runtime the variable's just been replaced with a reference to that space relative to the stack pointer.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Interesting. So if I do not initialize the variable inside the condition/while loop, then pretty much the exact same thing happens?

Also, out of curiosity, how do you guys know about that? Personal experience, random tidbits here and there or reading the full c++/c# specifications?
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...

This topic is closed to new replies.

Advertisement