C++ 静态数组初始化:内联初始化是否保留 space?
C++ static array initiation: does inline initialization reserve space?
在C++中,以下两个声明是否等价?
static volatile uint16_t *ADCReadings = (uint16_t[64]){0};
或
static volatile uint16_t ADCReadings[64] = {0};
我正在为 AVR 上的 ISR 内部和外部使用的缓冲区保留 space。但是,尽管这很有趣,但我并不是想知道这是否是最好的方法 - 我想知道这两个声明之间的区别(如果有的话)是什么,以便我能更好地理解这一点.
我知道这两个不同的声明会产生不同大小的二进制文件,因此编译器似乎对它们的处理方式不同。
Are the following two declarations equivalent?
没有。 (uint16_t[64]){0}
是一个临时数组(复合文字*),如果您尝试编译第一行,您将得到不言自明的 warning/error:
warning: temporary whose address is used as value of local variable 'ADCReadings' will be destroyed at the end of the full-expression
(Clang)
error: taking address of temporary array
(Gcc)
所以,第一行的ADCReadings
变成了悬空指针。将其用作指向缓冲区的指针会调用未定义的行为。
Gcc 手册reads:
In C, a compound literal designates an unnamed object with static or automatic storage duration. In C++, a compound literal designates a temporary object that only lives until the end of its full-expression. As a result, well-defined C code that takes the address of a subobject of a compound literal can be undefined in C++, so G++ rejects the conversion of a temporary array to a pointer.
Is this true even at global scope?
在这种情况下,Gcc 和 Clang 不会生成 warnings/errors。 Gcc手册进一步阅读:
As an optimization, G++ sometimes gives array compound literals longer lifetimes: when the array either appears outside a function or has a const-qualified type. ... if foo
were a global variable, the array would have static storage duration. But it is probably safest just to avoid the use of array compound literals in C++ code.
所以看起来在全局范围内 ADCReadings
不会悬空。
I do know that the two different declarations produce different sized binaries
在第一种情况下,复合文字数组进入 .bss
部分,ADCReadings
进入 .data
部分:
0000000000004040 l O .bss 0000000000000080 ._0
0000000000004010 g O .data 0000000000000008 ADCReadings
ADCReadings
是指向 ._0
.
的指针
第二种情况,ADCReadings
直接进入.bss
:
0000000000004040 g O .bss 0000000000000080 ADCReadings
这也转化为如何使用 ADCReadings
。下面的简单函数:
uint16_t* foo() {
return ADCReadings;
}
编译成:
push rbp
mov rbp,rsp
mov rax,QWORD PTR [rip+0x2ed8] # 4010 <ADCReadings>
pop rbp
ret
和
push rbp
mov rbp,rsp
lea rax,[rip+0x2f08] # 4040 <ADCReadings>
pop rbp
ret
* Compound literals 不是标准 C++ 的一部分,一些编译器(Gcc、Clang)允许它们作为扩展。
在C++中,以下两个声明是否等价?
static volatile uint16_t *ADCReadings = (uint16_t[64]){0};
或
static volatile uint16_t ADCReadings[64] = {0};
我正在为 AVR 上的 ISR 内部和外部使用的缓冲区保留 space。但是,尽管这很有趣,但我并不是想知道这是否是最好的方法 - 我想知道这两个声明之间的区别(如果有的话)是什么,以便我能更好地理解这一点.
我知道这两个不同的声明会产生不同大小的二进制文件,因此编译器似乎对它们的处理方式不同。
Are the following two declarations equivalent?
没有。 (uint16_t[64]){0}
是一个临时数组(复合文字*),如果您尝试编译第一行,您将得到不言自明的 warning/error:
warning: temporary whose address is used as value of local variable 'ADCReadings' will be destroyed at the end of the full-expression
(Clang)
error: taking address of temporary array
(Gcc)
所以,第一行的ADCReadings
变成了悬空指针。将其用作指向缓冲区的指针会调用未定义的行为。
Gcc 手册reads:
In C, a compound literal designates an unnamed object with static or automatic storage duration. In C++, a compound literal designates a temporary object that only lives until the end of its full-expression. As a result, well-defined C code that takes the address of a subobject of a compound literal can be undefined in C++, so G++ rejects the conversion of a temporary array to a pointer.
Is this true even at global scope?
在这种情况下,Gcc 和 Clang 不会生成 warnings/errors。 Gcc手册进一步阅读:
As an optimization, G++ sometimes gives array compound literals longer lifetimes: when the array either appears outside a function or has a const-qualified type. ... if
foo
were a global variable, the array would have static storage duration. But it is probably safest just to avoid the use of array compound literals in C++ code.
所以看起来在全局范围内 ADCReadings
不会悬空。
I do know that the two different declarations produce different sized binaries
在第一种情况下,复合文字数组进入 .bss
部分,ADCReadings
进入 .data
部分:
0000000000004040 l O .bss 0000000000000080 ._0
0000000000004010 g O .data 0000000000000008 ADCReadings
ADCReadings
是指向 ._0
.
第二种情况,ADCReadings
直接进入.bss
:
0000000000004040 g O .bss 0000000000000080 ADCReadings
这也转化为如何使用 ADCReadings
。下面的简单函数:
uint16_t* foo() {
return ADCReadings;
}
编译成:
push rbp
mov rbp,rsp
mov rax,QWORD PTR [rip+0x2ed8] # 4010 <ADCReadings>
pop rbp
ret
和
push rbp
mov rbp,rsp
lea rax,[rip+0x2f08] # 4040 <ADCReadings>
pop rbp
ret
* Compound literals 不是标准 C++ 的一部分,一些编译器(Gcc、Clang)允许它们作为扩展。