Variable allocation, when?

This is a discussion on Variable allocation, when? within the C++ Programming forums, part of the General Programming Boards category; Just a little dumb question. You know, when declaring a variable it is pushed onto the stack and is then ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Variable allocation, when?

    Just a little dumb question.
    You know, when declaring a variable it is pushed onto the stack and is then accessed through some addressing relative the stack pointer, right?
    My question is, when a function is called are all variables pushed directly at the call or can they be pushed later if you wait with the declaration?
    Code:
    void Func(int Arg)
    {
       int Var1;
    
       if(Arg == something) return;
    
       int Var2;
    }
    In the exampple above, will Var2 always be pushed or only when Arg is not 'something'?
    I usually declare all my variables at the top to easily see what are used in the function, but for speed (duh!) it may be better to wait with the declaration until they are actually used?

    ...Just me being bored and having nothing better to think of...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Usually, on entering a function, the code produces a stack frame.....this is done by looking at what space is needed (maybe 10 DWORDs..sometimes more) and adjusting the stack pointer and base pointer to give the required stack room...

    In your code, the int declarations do nothing, and they most likely wont appear on the stack at all, but their space requirement would be taken into account if they were used.

    As for the return, yes - I guess the second int would have space allocated (if it was used - as it's not used, the optimiser may ignore it) as the logic test to return or not is done at runtime, so at compile time, the compiler would take a more prudent view

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Also, exactly how does the 'relative adressing' work? I guess you can't use a constant since that would screw things up if you push something else. If it's some kind of variable, where is it stored?

    Note: In the example above, I assumed that the compiler didn't do any kind of optimization, so the variable declarations remains .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Thanks Salem, that was a useful feature. I think I got it now. There is also a datapointer pointing to the variables in the current block, and they are referenced through constants relative the DP. The DP still points at the same location even if the SP is changed.
    I also learned how arguments/return values for functions are handled .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I thought that you had to declare the variables at the beginning of any block, before any other code...unless I've been misinformed, your code wouldn't work without some more brackets.
    Away.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by blackrat364
    I thought that you had to declare the variables at the beginning of any block, before any other code...unless I've been misinformed, your code wouldn't work without some more brackets.
    For C, yes. For C++, no. I prefer using the C style, because it looks like crap otherwise, and makes your code harder to follow usually.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    Originally posted by quzah
    For C, yes. For C++, no. I prefer using the C style, because it looks like crap otherwise, and makes your code harder to follow usually.

    Quzah.
    Agreed. I would rather declare vars and pointers at the beginning of any code... it just makes more sense that way, looks better, and is easier to read (like Quzah said).
    "What are you after - the vague post of the week award?" - Salem
    IPv6 Ready.
    Travel the world, meet interesting people...kill them.
    Trying to fix or change something, only guaruntees and perpetuates its existence.
    I don't know about angels, but it is fear that gives men wings.
    The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

    E-Mail Xei

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by quzah
    For C, yes. For C++, no. I prefer using the C style, because it looks like crap otherwise, and makes your code harder to follow usually.

    Quzah.
    It does make the code more understandable, but if the variable is a class with a constructor, then it's often more efficient to declare when needed.....


    Code:
    class foobar
    {
       foobar(){/*Do something*/}
    };
    
    void SomeFunc(bool b)
    {
       foobar foo;//constructor called no matter what
    
       if(!b)return;
    
       foobar bar;//constructor only called if needed
    
    }
    ...but for standard ints, char*s and the like, this doesnt matter and the extra clarity of having variables at the top of a block makes sense

  9. #9
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    If you need to access variables explicitly off the stack it is quite straight forward. However, what you have to remember is when the activation record is being constructed (before the ip is even loaded with the address of the function) the parameters of the functions are pushed from right to left. Therefore to access the very first parameter off the stack you would have to use indirect addressing, e.g. mov eax, [ebp + 8] The plus 8 is to skip the callers saved base pointer and the return address of the function.
    If you really want to declare a variable within a function which might not be used, but you want to prevent it from being optimised out, you would declare it as volatile.

    Hope this is a bit more help for ya.
    Be a leader and not a follower.

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    There is a reason it is like that in c++. the reason is that most times a variable is declared a constructor is called ( sometimes many). This is an expensive performance hit for an unused variable. Same goes for its destruction.
    Scott meyers wrote an article on this in 1 of his books.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 04:13 AM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 04:31 PM
  4. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 01:30 PM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 07:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21