Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Every programmer that uses a language with C's memory model (and preferably absolutely every programmer) should know the following by heart:

    |-------| - max address
    |STACK ↓|
    |  SP   | ← stack pointer
    | ..... |
    | ..... |
    |HEAP  ↑|
    |-------|
    |other  |
    |mapped |
    |memory |
    |-------| - zero
Barring alternate memory managers (if you use one, you know the diagram and are now writing a post why it's wrong), stack grows down, heap grows up. When you call a function, SP is decremented by the total size of the stack variables in the called function, the address of the next instruction in the current function is written at SP, and each variable in the called function is written at SP - x, where x is an offset calculated by the compiler. When the function returns, the memory isn't cleared, the address of where we left off the caller is read, SP is incremented to its previous value, and the processor resumes from that point. The "push" and "pop" cpu instructions don't allocate memory, they're just a shorthand to decrement SP and copy a value.

For a fun demonstration, compile this C code with -O0 (optimizations off, or debug build in Visual Studio, IIRC):

    #include <stdio.h>

    int foo(int unused) {
        int a;
        return a;
    }

    int bar(int x) {
        int b;
        b = x;
        return b;
    }

    int main(int argc, char** argv) {
        printf("%d\n", foo());
        bar(10);
        printf("%d\n", foo());
        return 0;
    }


Not always. Stacks can grow up in some CPUs. http://stackoverflow.com/questions/664744/what-is-the-direct...

And then there's Itanium, which has two separate stacks, one growing up and one growing down...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: