Thread: memory allocation on the stack

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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"

  2. #2
    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?

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