Segmentation error on transition to VLA array

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;//or something else;
        }
}

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?

+4
1

(VLA) - C99, gcc ++, C99 - VLA undefined, C99 6.8.6.1 goto:

goto , .

clang gcc 4.9 :

error: goto into protected scope
    goto loop;
    ^

note: jump bypasses initialization of variable length array
            unsigned char tmp[remaining];
                          ^

gcc: VLA VM ++.

++ 6.7 [stmt.dcl], :

, , . , 87 , , , , , cv- (8.5). [:

void f() {
    // ...
    goto lx; // ill-formed: jump into scope of a
ly:
    X a = 1;
    // ...
lx:
    goto ly; // OK, jump implies destructor
             // call for a followed by construction
            // again immediately following label ly
}

-end ]

+5

All Articles