gcc抱怨:可变大小的对象可能未被初始化 [英] gcc complains: variable-sized object may not be initialized

查看:731
本文介绍了gcc抱怨:可变大小的对象可能未被初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这些,他们不回答我的问题:

变量大小的对象可能未被初始化



C编译错误:可变大小的对象不能被初始化 p>

错误:变量大小的对象可能未被初始化。但为什么?




我在编写一些相当便携的 c 代码:

  int main()
{
const int foo = 13;
int bar [foo] = {0};
返回0;
}



我得到一个使用以下任一方法编译为 c 代码时,可能未初始化变量大小的对象错误




  • gcc 4.3.4

  • arm-linux-gnueabi-gcc 4.4.5



如果我在 VS2008 c >我得到一个稍微不同的错误C2057:预期的常量表达式




我明白在这里, c 代码编译器无法识别 const int foo = 13; 是真正的常量;例如我们可能有

  void a(int fool)
{
const int foo = fool;
int bar [foo] = {0};
}


我也意识到与gcc编译器不同 VS2008 编译器没有 C99可变长度数组。而且这个MS显然还没有提到任何未来的支持。




然而, cpp 代码编译与 gcc MS 编译器是完全不同的/更聪明的?!





关于 gcc c 代码编译器的




(注意:在这最后一种情况下, MS c 代码编译失败;与 int bar [foo] = {0};

解决方案

一致。 p> C99§6.7.8初始化表示:


要初始化的实体的类型应为一个未知大小的数组或一个不是可变长度数组类型的对象类型。

初始化无效C。



的唯一方式是将[size] 改为 not 是一个VLA是 size 是一个整型常量表达式(第6.7.5.2节)。你有什么不是整数常量表达式,所以你有一个VLA:


如果大小不存在,数组类型是不完整的类型。如果大小是*而不是表达式,那么数组类型是一个可变长度数组类型的未指定大小
,它只能在具有函数原型范围的声明中使用,这样的数组是
但是是完整的类型。如果大小是整型常量表达式,并且元素类型具有已知的常量大小,则数组类型不是可变长度数组类型;
另外,数组类型是一个变长数组类型。

部分§6.6/ 6 常量表达式将它们定义为:


整数常量表达式应该具有整数类型,并且应该只有操作数
是整数常量,枚举常量,字符常量,sizeof
表达式,其结果是整型常量,以及浮点常量,它们是
转换的立即操作数。在整型常量表达式中铸造运算符应该只有
将算术类型转换为整数类型,除了作为sizeof
运算符的一个操作数的一部分。



I've looked at these and they do not answer my question:

variable-sized object may not be initialized

C compile error: "Variable-sized object may not be initialized"

Error: Variable-sized object may not be initialized. But why?


I am trying to write some fairly portable c code:

int main () 
{
    const int foo=13;
    int bar[foo]={0};
    return 0;
}


I get a variable-sized object may not be initialized error when compiling as c code using either:

  • gcc 4.3.4
  • arm-linux-gnueabi-gcc 4.4.5

And if i compile it as c in VS2008 i get a slightly different error C2057: expected constant expression


I understand that here, the c code compiler is not recognising const int foo=13; to be truely constant; for example we might have

void a(int fool) 
{    
    const int foo=fool;
    int bar[foo]={0};
}


I also realise that unlike the gcc compilers, the VS2008 compiler has no concept of C99 variable-length arrays. And that MS apparently has not mentioned any future support.


And yet, cpp code compilation with either gcc or MS compilers is altogether different/cleverer ?!


And also what i do not understand regarding the gcc c code compiler is:


(NB: in this last case, MS c code compilation fails; consistently as with int bar[foo]={0};)

解决方案

C99 §6.7.8 Initialization says this:

The type of the entity to be initialized shall be an array of unknown size or an object type that is not a variable length array type.

So your initialization is invalid C.

The only way for type a[size] to not be a VLA is for size to be an integer constant expression (§6.7.5.2). What you have there is not an integer constant expression, so you have a VLA:

If the size is not present, the array type is an incomplete type. If the size is * instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations with function prototype scope such arrays are nonetheless complete types. If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Part §6.6/6 Constant expressions defines them as:

An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.

这篇关于gcc抱怨:可变大小的对象可能未被初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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