Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C : stack memory, goto and "jump into scope of identifier with variably modified type",

Tags:

I found that this refuses to compile :

int test_alloc_stack(int size){     if(0) goto error; // same issue whatever conditional is used     int apply[size];     give_values(apply,size);     return 1;     error:         return 0; } 

The error I get is : "jump into scope of identifier with variably modified type". Eliminating the line with "goto" and the jump to error solves the issues.

If I use dynamic allocation for apply, then the problem also disappear. This compiles fine:

 int test_alloc_heap(int size){     if(0) goto error;     int * apply = calloc(sizeof(int),size);     give_values(apply,size);     free(apply);     return 1;     error : return 0; } 

What is going on ?

like image 383
Vince Avatar asked Dec 18 '13 09:12

Vince


1 Answers

The declaration:

int apply[size]; 

creates a variable length array. When it goes out of scope, the compiler must produce some code that cleans up the allocation for that array. Jumping into the scope of such an object is forbidden I imagine because some implementations might need to arrange for some initialization that the clean up code would require, and if you jump into the scope the initialization would be bypassed.

If you change to a dynamic allocation, the initialization and clean up become your responsibility instead of the compiler's.

like image 190
Michael Burr Avatar answered Sep 27 '22 22:09

Michael Burr