C:堆栈内存,GOTO和"跳进标识符的范围具有可变类型&QUOT ;, [英] C : stack memory, goto and "jump into scope of identifier with variably modified type",

查看:1038
本文介绍了C:堆栈内存,GOTO和"跳进标识符的范围具有可变类型&QUOT ;,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,这种拒绝编译:

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

这是怎么回事?

推荐答案

声明:

int apply[size];

创建一个可变长度的数组。当它超出范围时,编译器必须产生一些code来清理该数组分配。跳进这样的对象的范围是被禁止我想象的,因为一些实现可能需要为一些初始化的清理code将需要安排,如果你跳进范围的初始化将被绕过。

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.

这篇关于C:堆栈内存,GOTO和"跳进标识符的范围具有可变类型&QUOT ;,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆