It's a follow-up of Redeclaring of an array in loop in C with some of my additional observation.
Consider following two examples:
#include <stdio.h>
int main(void)
{
int i = 1;
while (i <= 10) {
int t[10];
if (i == 1)
t[0] = 100;
else {
t[0]++;
printf("i = %d, t[0] = %d\n", i, t[0]);
}
i++;
}
return 0;
}
with result as expected:
i = 2, t[0] = 101
i = 3, t[0] = 102
i = 4, t[0] = 103
i = 5, t[0] = 104
i = 6, t[0] = 105
i = 7, t[0] = 106
i = 8, t[0] = 107
i = 9, t[0] = 108
i = 10, t[0] = 109
and second one (slightly different) with VLA (introduced in C99):
#include <stdio.h>
int main(void)
{
int i = 1;
while (i <= 10) {
int t[i];
if (i == 1)
t[0] = 100;
else {
t[0]++;
printf("i = %d, t[0] = %d\n", i, t[0]);
}
i++;
}
return 0;
}
What I am getting for second example (in gcc 4.4.7) is something strange to me:
i = 2, t[0] = 101
i = 3, t[0] = 102
i = 4, t[0] = 103
i = 5, t[0] = 1
i = 6, t[0] = 2
i = 7, t[0] = 3
i = 8, t[0] = 4
i = 9, t[0] = 1637935601
i = 10, t[0] = 1637935602
Is there any rule/reference in C standard for VLAs declared inside loop or is it just a compiler's bug ?