James Kanze said:
messagenews:76a6f752-8506-48f4-9ad9-2daaa7efe678@h11g2000yqb.googlegroups..com...
Could you be a bit more specific? Which part of the standard
would prohibit the use of heap storage for auto variables?
§3.7.2 Automatic storage duration
[...]
"The storage for these objects lasts until the block in which
they are created exits."
This concerns only the life time, not the location.
Is there any reason why an auto variable created on the heap could not
be automatically deleted at the end of the block?
Yes. The authors chose a different vocabulary, and used it
consistently. They could have just as easily called it "stack
duration" and "heap duration", and then used those terms
consistently. The terms the standard uses are probably
preferable in the context of the standard, since they seem to
relate to "duration" more than "stack" or "heap" do, but there's
no problem with using "stack" and "heap" in less formal
use---it's quite clear what is meant.
I think there is another reason. The standard is only concerned
about the life time, not about the implementation.
Stack is more than life time, it is also a certain region of
memory, a certain ordered sequence of addresses, etc.
Since the standard is only concerned about the lifetime,
they formulated it in such a way that an implementation
is free to use the most efficient way for storage. In many cases
this means for auto variables stack and for dynamic variable heap,
but there might be cases were it is different.
So, speaking of stack and heap, without saying whether you refer
only to the lifetime, or also to the other aspects of stack and heap,
makes it less clear what is meant.