Managing Stack space?

This is a discussion on Managing Stack space? within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to find out how a compiler manages the stack space. Does it only allocate the stack ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Question Managing Stack space?

    Hi

    I am trying to find out how a compiler manages the stack space. Does it only allocate the stack memory once on program startup or does it allocate stack memory on every function call/startup? I am specifically interested in how it manages to keep local variables from colliding in memory when for ex: a function is calling it self.

    Thanks in advance
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I am specifically interested in how it manages to keep local variables from colliding in memory for ex: a function is calling it self.
    When a function calls itself a new activation record is pushed onto the stack[*]. The activation record has new memory to allocate local variables, so there will be no collisions. When the function returns, the activation record is popped off of the stack and memory reclaimed for other purposes. The control flow returns to the calling function. It helps to remember that even though a function appears to call itself, it really creates a copy of iteself and calls that[&].

    >Does it only allocate the stack memory once on program startup or does it allocate stack memory on every function call/startup?
    Typically a new program will be allocated a large chunk of memory that it splits up into blocks for static variables, stack space, and heap space. As functions are called and return they use this memory. To my knowledge, most implementations do not allocate and release memory every time a function is called and returns. At least not in the way you're thinking.
    [*] I say stack for the sake of explanation, there may not really be one.

    [&] Once again, for the sake of explanation. Actual implementations may do something different.
    My best code is written with the delete key.

  3. #3
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Hi,

    I'm interested in learning all these things about what's really going on behind the scenes. Can you recommend any websites etc. that have more about the lower level stuff?

    Thanks.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Yes thanks, I would I also appreciate such information.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can google it and see, but the inner workings of compilers and operating systems is hard to find on the net. I got most of what I know from books and source code, not to mention a few knowledgeable friends.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  2. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 08:32 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

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