• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Stack space - allocating local variables

When entering a function, does stack space get allocated for all local variables for that function even if they are burried in a if / else block?  Or do local
variables only get pushed on the stack when they become "in scope" within
the function while the function is executing?  For example:

function1()
{
    int i=0;
}

funtion2()
{
    bool bTest=false;
    if (bTest)
        int i=0;
 }

Will space ever be allocated on the stack for the variable i in function2 since the if is never entered?
If so, there is no advantage to declaring your variables within a if block, right?  It would be the same as declaring it at the beginning of the function.  All
variables within the if / else blocks will be allocated on the stack even if
those blocks are never entered during program flow.  Or do the variables
within the if blocks only get put on the stack if that block of code is
entered during execution?

Thanks.

Timothy Dean
MobileDataforce
0
kevinbenedict
Asked:
kevinbenedict
  • 4
1 Solution
 
AxterCommented:
Hi kevinbenedict,
> >does stack space get allocated for all local variables for that function
> >even if they are burried in a if / else block?
Yes, but that doesn't mean that the constructor is called at that time.

David Maisonave :-)
Cheers!
0
 
AxterCommented:
kevinbenedict,
> >Will space ever be allocated on the stack for the variable i in function2
> >since the if is never entered?

It should under a normal compile, however with optimization settings, the compiler could optimize away the code, in which case it would allocate space on the stack for it.

David Maisonave :-}
0
 
AxterCommented:
kevinbenedict,
> >If so, there is no advantage to declaring your variables within a if
> >block, right?

There is an advantage.  Even if stack pace is allocated, the constructor is not called for that variable until needed, so your function will run more efficient.

David Maisonave :-}
0
 
waysideCommented:
> If so, there is no advantage to declaring your variables within a if block, right?

It generally makes your code more readable and more maintainable to declare the variables as close to possible to where they are being used.

So regardless of whether you save any space on the stack or not, it is good programming practice to declare the variables inside of whatever scope they are used in, if the variable is only used within that scope.
0
 
AxterCommented:
>>It generally makes your code more readable and more maintainable to declare the variables as close to possible to where they are being used.

I agree with you totally.  However, there are some programmers who think it's easier to maintain code when all the variables are on the top (start) of the function.
A lot of old C-programmers think that way, and no matter what you tell them, they refuse to see the benefits of lazy construction.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now