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

后端 未结 3 679
别那么骄傲
别那么骄傲 2020-12-07 00:36

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];
         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 01:06

    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.

提交回复
热议问题