I need help with the "stack" please

This is a discussion on I need help with the "stack" please within the C++ Programming forums, part of the General Programming Boards category; Could someone give me a link to a site that explains how the stack works, please that would be greatly ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    I need help with the "stack" please

    Could someone give me a link to a site that explains how the stack works, please that would be greatly appreciated.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    "Last on, First off" type of memory management.

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    I hope this is right but this is how I understand it.
    func1()
    {
    return 0;
    }

    func2()
    {
    func1();
    return 0;
    }

    func3
    {
    func2()
    return 0;
    }

    void main()
    {
    func3();
    }

    func3()'s return type and its parameters( I don't have any) are popped onto the stack first then it calls func2() its parameters and return type are on next. Then func1() is called its parameters and return type on popped onto the stack last. Then going back from func1() the are popped off from the stack.
    Last edited by aresashura; 12-16-2001 at 08:32 PM.

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I am kind of confused though, what happens when a function is called I mean in terms of memory and the stack? Please make this as simple as possible.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I mean in terms of the function branching off to the stack, how does this happen?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    A function never "branches off" to the stack. The stack is a special area of memory, which is used for most types of data storage.

    A stack is, conceptually, like a stack of books. You can put books on the stack (in programming, this is "pushing" onto the stack). You can take a book off of the stack ("popping"), and the book you get will be the last one you pushed.

    The stack is nothing more than a special region of memory set aside for local variable storage. Each program under Windows is given its own stack.

    Function branching USES the stack (to store the return address -- the address that the function should go back to when it finishes) but you can't branch TO the stack, that would crash the program.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    Each program under Windows is given its own stack
    It is true. More exactly for Windows, each executing thread has its own stack (local variables) and all threads share heap (global variables). Sharing global variables by concurrent threads is very problematic, therefore each thread may allocate its "thread local storage" (TLS).

    Function branching USES the stack (to store the return address -- the address that the function should go back to when it finishes)
    Not only return address. When you call a function, the caller (function that calls the function) pushes the arguments onto stack, just like the projectiles into pistol's cartridge. Depending on calling convention, after called function has finished, caller may decide to pop these arguments back, or leave this job to callee. This type of convention is named __cdecl and is used widely in C Runtime Library. All other conventions force callee (the called function) to pop these arguments back from stack. These include __stdcall (used by WinAPI), __fastcall and thiscall. Argumets are allways pushed in reverse order, that is the rightmost parameter is pushed first.

    That's why you can use functions with elipsis (...), ex. printf (), only with __cdecl. Only caller knows how many arguments were really pushed and can pop them back eventually.

  8. #8
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    ok I got the pistol analogy, well sort of, could you like tell me what it is without getting really deep because I am just learning about the stack and all,.... Like for example, tell me what happens in memory when a program is compiled and the program is run.....please how the function returns and all that....please use the easiest analogy possible anybody please...I am desperate here.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  9. #9
    Unregistered
    Guest
    anybody please............................................ ..............

Popular pages Recent additions subscribe to a feed

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