Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

goto instruction to jump ahead of variable length array

Tags:

c

int main() {
    int sz = 10; 
    goto end;
    char bytes[sz];
end:
    return 0;
}

I get the following error at compilation. I use gcc with C99 standard.

test.c: In function ‘main’:
test.c:3:2: error: jump into scope of identifier with variably modified type
test.c:5:1: note: label ‘end’ defined here
test.c:4:7: note: ‘bytes’ declared here
like image 374
sunil Avatar asked Apr 07 '14 19:04

sunil


1 Answers

It is forbidden by the standard:

C99 standard, paragraph 6.8.6.1

Constraints

[...] A goto statement shall not jump from outside the scope of an identifier having a 
      variably modified type to inside the scope of that identifier.

Your goto skips the line that allocates your bytes array at runtime. That is not allowed.

You could limit the scope of bytes by surrounding it with curly braces, put the allocation before the goto and label, or not use goto at all.

To put it more plainly, once bytes is allocated, you are now "inside" the scope. Before the allocation, you are "outside" the scope. So you can't "jump from outside the scope" to "inside the scope".

like image 182
Engineer2021 Avatar answered Oct 17 '22 11:10

Engineer2021