Thread: memory allocation on the stack

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    596

    memory allocation on the stack

    In C++, when a function is loaded are ALL of its statically allocated variables placed on the stack immediately, or are local variables put on the stack only when they are in scope?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The compiler calculates the size of the function's variables and upon entry the function adjusts the stack pointer by that much. At this point nothing is initialised though, it only contains garbage data.

    Then upon entry to each scope block (curly braces), variables inside that scope block are initialised (their constructor is called if one was provided).

    At the end of the scope block, those variables are destructed (their destructor is called if one was provided), but the stack space for them tends to exist until the end of the function.

    It's also possible for variables of non-overlapping scope to re-use the same portion of the stack.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by iMalc View Post
    The compiler calculates the size of the function's variables and upon entry the function adjusts the stack pointer by that much....
    It's also possible for variables of non-overlapping scope to re-use the same portion of the stack.
    So in light of the last sentence, the size of the stack allocated for a function may be less than the sum of the sizes of its variables, correct?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Variable lifetime matches variable scope, except in the case of static local variables, which have global lifetimes but local scope

    Quote Originally Posted by R.Stiltskin View Post
    So in light of the last sentence, the size of the stack allocated for a function may be less than the sum of the sizes of its variables, correct?
    Yes -- variables which have non-overlapping lifetimes might be placed at the same location in the activation record
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't think the standard actually says anything about how/when the space is allocated on the stack for local variables [or even that they have to be on the stack, as such - but some sort of stack-like construction is obviously necessary].

    In practice, what iMalc says holds true, but there is nothing saying the compiler is not ALLOWED to allocate more space on the stack in the middle of a function. It's just that the compiler would have to issue extra instructions to achieve this, and it's just making things more complicated than calculating the overall space needed.

    Of course, variables do not HAVE to reside on the stack at all. Local variables may well be stored entirely in registers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by matsp View Post
    Of course, variables do not HAVE to reside on the stack at all. Local variables may well be stored entirely in registers.

    --
    Mats
    Heh. I hadn't thought of that at all when I posted the question but now that you mention it that seems pretty obvious.

    Thanks everyone.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't think the standard actually says anything about how/when the space is allocated on the stack for local variables [or even that they have to be on the stack, as such - but some sort of stack-like construction is obviously necessary].
    Or even if there is a stack. Yes, something stack-like is necessary, but a limited implementation could actually get by with something different.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yup 'sall true!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack-based allocation
    By eXodus31337 in forum C++ Programming
    Replies: 10
    Last Post: 01-05-2009, 11:03 PM
  2. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM