Hacker News new | ask | show | jobs
by Sir_Cmpwn 2823 days ago
You're joking. There's a very serious distinction between the stack and the heap - perhaps they live in the same memory but they are used very differently and if you mix them up your things will break.
2 comments

There is no hardware distinction between stack memory and heap memory.

In fact C teaches a model of a semi-standard virtual architecture - loosely based on the DEC PDP7 and/or PDP11 - which is long gone from real hardware.

Real hardware today has multiple abstraction layers under the assembly code, and all but the top layer is inaccessible.

So there's no single definitive model of "How computers work."

They work at whatever level of abstraction you need them to work. You should definitely be familiar with a good selection of levels - and unless you're doing chip design, they're all equally real.

There definitely is, unless there's a hardware heap pointer and hardware allocation and deallocation instructions, as there are for the stack :)
This isn't true. Modern instruction sets have clearly been influenced by and designed to optimize the C ABI.
...like returning a pointer to the stack:

    char *dupstr(const char *src) {
       char new[1024];
       strlcpy(new, src, 1024);
       return new;
    }
stack_ret.c:5:10: warning: function returns address of local variable [-Wreturn-local-addr] return new;
Why is this a warning and not an error? Are there situations in which you would want to return the address of a local variable?