Hacker News new | past | comments | ask | show | jobs | submit login

A compiler can reuse memory as much as it likes -- but only if the visible behavior of the program is consistent with the language requirements.

If you write:

    {
        int n = 42;
        printf("%d\n", n);
    }
in the abstract machine, `sizeof (int)` bytes are allocated on entry to the block and deallocated on exit, but a compiler can legally replace the entire block with `puts("42")` and not allocate any memory for `n`.

Memory for objects defined in nested blocks is logically allocated on entry to the block, but compilers commonly merge the allocation into the function entry code. Even so, objects in parallel blocks can certainly share memory:

    int main(void) {
        {
            int a;
        }
        {
            int b; // might share memory with a
        }
    }
Logically, memory for `a` is allocated on entry to the first inner block, and memory for `b` on entry to the second inner block. Compilers will typically allocate all the memory on entry to `main`, but can use the same address for `a` and `b`.



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

Search: