dhein
dhein

Reputation: 6555

Direct stack and heap access; Virtual- or hardware- level?

When I'm on SO I read a lot of comments guiding (Especially in C)

"dynamic allocation allways goes to the heap, automatic allocation on the stack"

But especially regarding to plain C I disaggree with that. As the ISO/IEC9899 doesn't even drop a word of heap or stack. It just mentions three storage duriations (static, automatic, and allocated) and advises how each of them has to be treat.

What would give a compiler the option to do it even wise versa if it would like to.

So my question is:

Are the heap and the stack physical existing that (even if not in C) a standardized language can say "... has to happen on heap and ... on the stack"?

Or are they just a virtuell system of managing memory access so that a language can't make rules about them, as it can't even be ensured the enviroment supports them?

In my knowledgebase only the second would make sense. But I read allready many times people writing comments like "In language XY this WILL happen on the stack/heap". But if I'm right this had to be indeterminable as long the language isn't just made for such systems which guarantee to have a stack and heap. And all thoose comments would be wrong.

Thats what lead me to ask about this question. Am I that wrong, or is there a big error in reasoning going around about that?

Upvotes: 2

Views: 187

Answers (1)

Colonel Thirty Two
Colonel Thirty Two

Reputation: 26549

You are correct in that the C spec doesn't require the use of a heap or stack, as long as it implements the storage classes correctly.

However, virtually every compiler will use stacks for automatic variables and heaps for allocated variables. While you could implement a compiler that doesn't use a stack or heap, it probably wouldn't perform very well and wouldn't be familiar to most devs.

So when people say "always", they really mean "virtually always".

Upvotes: 2

Related Questions