(e-mail address removed) (Michael) wrote...
In the below example, I beleive that the char array buffer[40] should
only occupy 40 bytes of the stack within the inner braces.
As far as the language specification is concerned automatic data will
only be allocated at the point of declaration, and will expire at the
end of the enclosing block scope.
No. You gets confused with visibility and reserved space. It is
completely legal theat an compuler will allocate the maximum used
space in asingle block for auto variables.
void f(void) {
if (x1)
{ int a[100]; }
if (x2)
{ int a; }
if (x3)
{
struct st {
int a, b, c, d;
double dd[999];
} s[10];
}
}
The compiler may or may not eleminate any unused variable - so giving
f() as above it may even eleminate any call of f() when the definition
of the function is visible, it may eleminate the whole body of the
fuction when it has nothing more as above, reducing it to a simple
return.
Assuming that each inner block has code that works with the variables
defined there it may allocate one single memory block big enough to
hold the biggest of all memory areas only while overlapping the
shorter ones, it may simply add the sizes and reserve the whole
storage or it can do whatever it likes to have the memory needed in
each block available. The only that the compiler will guarantee when
it defines itself as standard compatible is to have each variable only
visible in the block it is defined.
It may change its behavior depending on flags given to it at
compiletime. It may change its behavior between subversions or full
versions of itself.
There must not even a stack available. Any possible memory handling to
reserve a properitary chunk of memory that is used only inside the
function and its inner blocks and making the variables visible only
inside the inner blocks fullyfies the requirements of the standard
perfectly.