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