The following example demonstrates the problem:
#include <cstdio>
int main()
{
unsigned int remaining=1;
goto loop;
while(remaining) {
unsigned char tmp[remaining];
printf("&tmp: %p\n",tmp);
loop:
remaining = 512;
}
}
Initially, the initialization of the “remaining” variable was a bit long, and I used gototo initialize it on a single line. However, now this example gives a segmentation error in a row printf.
It appears that the array is not initialized correctly.
Even gdb cannot print the address of the tmp array:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004005b8 in main () at test.cpp:11
11 printf("&tmp: %p\n",tmp);
(gdb) p tmp
$1 = 0xfffffffffffffe00 <error: Cannot access memory at address 0xfffffffffffffe00>
My gcc version:
gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
compilation with:
g++ -o testc test.cpp
If I remove goto or replace the variable array with a fixed array, the segmentation error will disappear. What is really going on?
Is this a gcc error? If combination gotoand variational arrays are not allowed, should there be a warning?