I replied with a lengthy explanation that you apparently
did not bother to READ.
The program dies because for a VLA I do not make the
The program dies because you aren't cleaning up the memory when a
block terminates.
optimization that the variable "size" is a loop invariant,
i.e. does not change within the loop.
If you replace
int tab[size];
with
int tab[size*i+1];
This tab could still be allocated in the same space. If the stack
grows downward, the starting address will be different. However, I
would expect the end address to be the same. Or very close---alignment
may cause it to jitter slightly.
gcc will ALSO produce different arrays for each iteration.
Oh really?
#include <stdio.h>
#include <string.h>
void test(int n, size_t size)
{
int i;
for(i = 0; i < n; i++) {
unsigned char vla[size+256*i];
memset(vla, (i & 255), size);
printf("step %d: vla address range == [%p,%p)\n", i,
(void *) vla, (void *) vla + size + 256 * i);
}
}
int main(void)
{
test(10, 256*1024L);
return 0;
}
step 0: vla address range == [0xbffbf990,0xbffff990)
step 1: vla address range == [0xbffbf890,0xbffff990)
step 2: vla address range == [0xbffbf790,0xbffff990)
step 3: vla address range == [0xbffbf690,0xbffff990)
step 4: vla address range == [0xbffbf590,0xbffff990)
step 5: vla address range == [0xbffbf490,0xbffff990)
step 6: vla address range == [0xbffbf390,0xbffff990)
step 7: vla address range == [0xbffbf290,0xbffff990)
step 8: vla address range == [0xbffbf190,0xbffff990)
step 9: vla address range == [0xbffbf090,0xbffff990)
Different starting address, same end. Obviously, someting like alloca
is going on, except that the allocation is tied to the block, not to
the function.
As I explained (and repeat here again) this is an optimization
that I do not do and gcc does.
Bahahahaha. I don't think what gcc is doing is called optimization.
It's called getting the semantics right. That's what gcc is doing (and
not you).