The following example demonstrates the issue:
#include <cstdio>
int main()
{
unsigned int remaining=1;
goto loop;
while(remaining) {
unsigned char tmp[remaining];
printf("&tmp: %p\n",tmp);
loop:
remaining = 512;//or something else;
}
}
Initially, the initialization of "remaining" variable was a bit long and I used goto
to initialize it on one line. However, now this example gives segmentation fault on the printf
line.
It looks like the array is not initialized properly.
Even gdb cannot print the tmp array's address:
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
compiling with:
g++ -o testc test.cpp
If I remove the goto, or replace the variadic array with a fixed array, the segmentation fault is gone. What is happening actually?
Is this a gcc bug? If the combination of goto
and variadic arrays is not allowed, there should be a warning?