Sign in to follow this  

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

This topic is 3489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Quote:
Original post by Koobazaur
Are they allocated on the stack when the function is called

Every C++ compiler I tried does that, yes.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

This topic is 3489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this