最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - "variable-sized object may not be initialized" error message when number of elements are calculated w

programmeradmin3浏览0评论

With GCC 11.4 (-std=c99) on Linux WSL, there is an compile error as in example 2) in the code below. I have not gotten this kind of error with other compilers such as TASKING, MULTI, ti-arm-clang.

Do you have any idea why this happens? Is calculation with float executed in runtime?

  1. int array1[(int)(1.0)]={0};//no error
  2. int array2[(int)(1.0+1.0)]={0};//error: variable-sized object may not be initialized
  3. int array3[(int)(1.0)+(int)(1.0)]={0};//no error
  4. int array5[(int)(1.f)]={0};//no error

With GCC 11.4 (-std=c99) on Linux WSL, there is an compile error as in example 2) in the code below. I have not gotten this kind of error with other compilers such as TASKING, MULTI, ti-arm-clang.

Do you have any idea why this happens? Is calculation with float executed in runtime?

  1. int array1[(int)(1.0)]={0};//no error
  2. int array2[(int)(1.0+1.0)]={0};//error: variable-sized object may not be initialized
  3. int array3[(int)(1.0)+(int)(1.0)]={0};//no error
  4. int array5[(int)(1.f)]={0};//no error
Share Improve this question edited Mar 24 at 12:49 Mat 207k41 gold badges402 silver badges418 bronze badges asked Mar 23 at 12:53 堀良寛堀良寛 311 bronze badge 1
  • 2 This feels like an XY question, since there obvious ways to fix the failing code, some of them even presented in this question itself. – John Bollinger Commented Mar 23 at 13:41
Add a comment  | 

1 Answer 1

Reset to default 7

The C standard does not require implementations to perform floating-point arithmetic in constant expressions.

Likely, you are declaring these arrays outside of any function, so they have static storage duration. The size for a static array must be an integer constant expression. C 2024 6.6 specifies:

An integer constant expression shall have integer type and shall only have operands that are integer constants, named and compound literal constants of integer type, character constants, sizeof expressions whose results are integer constants, alignof expressions, and floating, named, or compound literal constants of arithmetic type 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 typeof operators, sizeof operator, or alignof operator.

Observe:

  1. int array1[(int)(1.0)]={0};//no error

(1.0) is a floating-point constant that is an immediate operand of a cast, so that is allowed. (Technically, 1.0 is inside parentheses, but we regard this as a grammatical grouping, not an operation.)

  1. int array2[(int)(1.0+1.0)]={0};//error: variable-sized object may not be initialized

1.0+1.0 has floating-point constants that are not the immediate operands of casts. The C standard does not require that + on floating-point operands be supported in constant expressions.

  1. int array3[(int)(1.0)+(int)(1.0)]={0};//no error

Here (1.0) is the immediate operand of a cast. Then the operands to + are two int values, which is supported in a constant expression.

  1. int array5[(int)(1.f)]={0};//no error

Here (1.f) is the immediate operand of a cast.

The C standard allows, but does not require, C implementations to accept other forms of constant expression. So some compilers may support floating-point arithmetic in constant expressions while others do not.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论