On this site there is the following paragraph (emphasis mine):
- automatic storage duration. The storage is allocated when the block in which the ob
From the N1570 draft of the C11 specification §6.2.4/7
For such an object that does have a variable length array type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration.
The specification then adds this helpful note:
Leaving the innermost block containing the declaration, or jumping to a point in that block or an embedded block prior to the declaration, leaves the scope of the declaration.
So the VLA is de-allocated when the execution goes outside the scope of the VLA, which includes the section in the same block before the declaration of the VLA.
Jumping to a point prior to the declaration can be done with a goto
statement. For example:
int n = 0;
while (n < 5)
{
top:
n++;
char array[n];
if (n < 2)
goto top;
}
In this code, the block is not exited when the goto
is executed. However, the value of n
changes, so a new array
needs to be allocated. That's the horribly convoluted situation that the specification is trying to support.