我刚刚在C++中遇到了一个令人讨厌的错误.所以我有一个寄存器和值列表,它们包含在一个结构中,然后这些结构在一个数组中初始化.但后来我不小心输了()而不是{}.这是一些测试代码:
#include <stdio.h>
struct reg_val {
unsigned reg;
unsigned val;
};
struct reg_val faulty_array[] = {
{0x5001, 0xff},
{0x5580, 0x01},
(0x5580, 0x02), //<- THIS LINE IS THE PROBLEM
(0x5589, 0x00), //<- AND THIS LINE
};
struct reg_val good_array[] = {
{0x5001, 0xff},
{0x5580, 0x01},
{0x5580, 0x02},
{0x5589, 0x00},
};
int main()
{
unsigned i;
unsigned faulty_size = sizeof(faulty_array) / sizeof(struct reg_val);
printf("Size of faulty array: %d\n", faulty_size);
for (i = 0; i < faulty_size; ++i) {
printf("faulty reg: %x val: %x\n", faulty_array[i].reg,
faulty_array[i].val);
}
unsigned good_size = sizeof(good_array) / sizeof(struct reg_val);
printf("\nSize of good array: %d\n", good_size);
for (i = 0; i < good_size; ++i) {
printf("good reg: %x val: %x\n", good_array[i].reg,
good_array[i].val);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我对C更熟悉,令我惊讶的是仍然用g ++编译:
$ g++ -Wall array.cc
array.cc:11: warning: left-hand operand of comma has no effect
array.cc:12: warning: left-hand operand of comma has no effect
array.cc:13: warning: missing braces around initializer for ‘reg_val’
$ ./a.out
Size of faulty array: 3
faulty reg: 5001 val: ff
faulty reg: 5580 val: 1
faulty reg: 2 val: 0 <-- the first value gets discarded as mentioned in the compiler warning
Size of good array: 4
good reg: 5001 val: ff
good reg: 5580 val: 1
good reg: 5580 val: 2
good reg: 5589 val: 0
Run Code Online (Sandbox Code Playgroud)
这个代码显然无法用C编译器编译,C++的不同之处在于C++编译器(虽然不情愿地)接受这个代码?
为了回答你的问题,我首先回答:为什么在C中编译失败?好吧,由于以下原因,它无法编译:
initializer element is not constant
Run Code Online (Sandbox Code Playgroud)
为了更好地衡量,让我们{}从 C 中删除 s:
struct reg_val faulty_array[] = {
{0x5001, 0xff},
{0x5580, 0x01},
0x5580, 0x02, //<- THIS LINE IS THE PROBLEM
0x5589, 0x00, //<- AND THIS LINE
};
Run Code Online (Sandbox Code Playgroud)
现在程序输出:
Size of faulty array: 4
faulty reg: 5001 val: ff
faulty reg: 5580 val: 1
faulty reg: 5580 val: 2
faulty reg: 5589 val: 0
Run Code Online (Sandbox Code Playgroud)
C 标准(和 C++)完全允许这样做。C(和 C++)展平大括号来初始化结构元素(这会回来)。您的代码在 C 中失败,因为具有静态存储持续时间的对象必须使用常量表达式或包含常量表达式的聚合初始值设定项进行初始化。C 不将其视为(0x5580, 0x02)常量表达式。
这(不幸的是)在 C++ 中编译,因为 C++ 将两个常量表达式之间的逗号运算符视为常量表达式,因此您的代码更像是:
struct reg_val faulty_array[] = {
{0x5001, 0xff},
{0x5580, 0x01},
0x02,
0x00,
};
Run Code Online (Sandbox Code Playgroud)
……这当然是允许的。
struct reg_val faulty_array[] = {
{0x5001, 0xff},
{0x5580, 0x01},
{0x02, 0x00},
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
258 次 |
| 最近记录: |