C错误:“初始化元素不是常数”用&,与+一起使用

时间:2021-04-03 04:27:24

With GCC I get the "initializer element is not constant" error for the second line of the following code:

使用GCC,我得到以下代码的第二行的“初始化元素不是常量”错误:

uint8_t gBuffer[512 + 4]; /* Data buffer */
uint8_t* gAlignedBuffer = (uint8_t*)(((uint32_t)gBuffer + 4) & 0xFFFFFFFCU);   /* Align buffer to 4-byte boundary */ 

However if I change & 0xFFFFFFFCU to + 0xFFFFFFFCU the code compiles ok:

但是,如果我将&0xFFFFFFFCU更改为+ 0xFFFFFFFCU,则代码编译正常:

uint8_t gBuffer[512 + 4]; /* Data buffer */
uint8_t* gAlignedBuffer = (uint8_t*)(((uint32_t)gBuffer + 4) + 0xFFFFFFFCU);   /* Align buffer to 4-byte boundary */ 

Why is this?

为什么是这样?

3 个解决方案

#1


2  

Apparently you are declaring your variables in file scope. File scope variables have static storage duration and require constant initializers.

显然,您在文件范围内声明了变量。文件范围变量具有静态存储持续时间并需要常量初始值设定

While your initializers don't exactly satisfy the most strict and narrow definition of address constant expression (as defined in the languages specification), they might still be supported by your specific compiler. The inconsistency you observe has no real reason to exist though. I'd guess that this is a quirk of that specific compiler.

虽然初始化程序并不完全满足地址常量表达式的最严格和最窄的定义(如语言规范中所定义),但您的特定编译器可能仍然支持它们。你观察到的不一致性并没有真正的理由存在。我猜这是特定编译器的一个怪癖。

#2


2  

Since these variables are file scope, they have static storage, so the behavior has to do with what kind of relocations your output file format supports (I assume it's ELF).

由于这些变量是文件范围,因此它们具有静态存储,因此行为与输出文件格式支持的重定位类型有关(我假设它是ELF)。

Basically, in this case, ELF objects support adding any number to an unknown address, but not applying a mask (which is basically what the & operation does). You will need to move the second statement into a function.

基本上,在这种情况下,ELF对象支持向未知地址添加任何数字,但不应用掩码(这基本上与&操作相同)。您需要将第二个语句移动到一个函数中。

#3


-2  

My suspicions is that something is happening with the operator precedence here. & is higher precedendce than + .

我怀疑这里运算符优先级正在发生。 &先于高于+。

#1


2  

Apparently you are declaring your variables in file scope. File scope variables have static storage duration and require constant initializers.

显然,您在文件范围内声明了变量。文件范围变量具有静态存储持续时间并需要常量初始值设定

While your initializers don't exactly satisfy the most strict and narrow definition of address constant expression (as defined in the languages specification), they might still be supported by your specific compiler. The inconsistency you observe has no real reason to exist though. I'd guess that this is a quirk of that specific compiler.

虽然初始化程序并不完全满足地址常量表达式的最严格和最窄的定义(如语言规范中所定义),但您的特定编译器可能仍然支持它们。你观察到的不一致性并没有真正的理由存在。我猜这是特定编译器的一个怪癖。

#2


2  

Since these variables are file scope, they have static storage, so the behavior has to do with what kind of relocations your output file format supports (I assume it's ELF).

由于这些变量是文件范围,因此它们具有静态存储,因此行为与输出文件格式支持的重定位类型有关(我假设它是ELF)。

Basically, in this case, ELF objects support adding any number to an unknown address, but not applying a mask (which is basically what the & operation does). You will need to move the second statement into a function.

基本上,在这种情况下,ELF对象支持向未知地址添加任何数字,但不应用掩码(这基本上与&操作相同)。您需要将第二个语句移动到一个函数中。

#3


-2  

My suspicions is that something is happening with the operator precedence here. & is higher precedendce than + .

我怀疑这里运算符优先级正在发生。 &先于高于+。