我试着运行你提供的代码片段 test.cpp
,通过gcc和clang以及多个优化级别:
steve@steve-pc /tmp> g++ -o test.gcc.O0 test.cpp
[ 0s828 | Jan 27 01:16PM ]
steve@steve-pc /tmp> g++ -o test.gcc.O2 -O2 test.cpp
[ 0s901 | Jan 27 01:16PM ]
steve@steve-pc /tmp> g++ -o test.gcc.Os -Os test.cpp
[ 0s875 | Jan 27 01:16PM ]
steve@steve-pc /tmp> ./test.gcc.O0
0 32764 [ 0s004 | Jan 27 01:16PM ]
steve@steve-pc /tmp> ./test.gcc.O2
0 0 [ 0s004 | Jan 27 01:16PM ]
steve@steve-pc /tmp> ./test.gcc.Os
0 0 [ 0s003 | Jan 27 01:16PM ]
steve@steve-pc /tmp> clang++ -o test.clang.O0 test.cpp
[ 1s089 | Jan 27 01:17PM ]
steve@steve-pc /tmp> clang++ -o test.clang.Os -Os test.cpp
[ 1s058 | Jan 27 01:17PM ]
steve@steve-pc /tmp> clang++ -o test.clang.O2 -O2 test.cpp
[ 1s109 | Jan 27 01:17PM ]
steve@steve-pc /tmp> ./test.clang.O0
0 274247888 [ 0s004 | Jan 27 01:17PM ]
steve@steve-pc /tmp> ./test.clang.Os
0 0 [ 0s004 | Jan 27 01:17PM ]
steve@steve-pc /tmp> ./test.clang.O2
0 0 [ 0s004 | Jan 27 01:17PM ]
steve@steve-pc /tmp> ./test.clang.O0
0 2127532240 [ 0s002 | Jan 27 01:18PM ]
steve@steve-pc /tmp> ./test.clang.O0
0 344211664 [ 0s004 | Jan 27 01:18PM ]
steve@steve-pc /tmp> ./test.clang.O0
0 1694408912 [ 0s004 | Jan 27 01:18PM ]
这就是有趣的地方,它清楚地显示出clang O0构建正在读取随机数字,可能是堆栈空间。
我立刻打开我的IDA看看发生了什么:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3;
__int64 v4;
int result;
unsigned int v6;
unsigned int v7;
unsigned __int64 v8;
v8 = __readfsqword(0x28u);
v7 = 0;
bar::bar((bar *)&v6);
v3 = std::ostream::operator<<(&std::cout, v7);
v4 = std::operator<<<std::char_traits<char>>(v3, 32LL);
result = std::ostream::operator<<(v4, v6);
if ( __readfsqword(0x28u) == v8 )
result = 0;
return result;
}
现在,
bar::bar(bar *this)
是做什么的?
void __fastcall bar::bar(bar *this)
{
;
}
嗯,什么也没有。我们不得不使用汇编语言:
.text:00000000000011D0
.text:00000000000011D0 public _ZN3barC2Ev
.text:00000000000011D0 _ZN3barC2Ev proc near
.text:00000000000011D0
.text:00000000000011D0 var_8 = qword ptr -8
.text:00000000000011D0
.text:00000000000011D0
.text:00000000000011D0 55 push rbp
.text:00000000000011D1 48 89 E5 mov rbp, rsp
.text:00000000000011D4 48 89 7D F8 mov [rbp+var_8], rdi
.text:00000000000011D8 5D pop rbp
.text:00000000000011D9 C3 retn
.text:00000000000011D9
.text:00000000000011D9 _ZN3barC2Ev endp
所以,这只是一个无用的构造函数,它基本上做的就是this = this
。但我们知道它实际上正在加载随机未初始化的堆栈地址并打印它。
如果我们明确为这两个结构提供值会怎样呢?
#include <iostream>
struct foo {
foo() = default;
int a;
};
struct bar {
bar();
int b;
};
bar::bar() = default;
int main() {
foo a{0};
bar b{0};
std::cout << a.a << ' ' << b.b;
}
运行clang,出了点小问题:
steve@steve-pc /tmp> clang++ -o test.clang.O0 test.cpp
test.cpp:17:9: error: no matching constructor for initialization of 'bar'
bar b{0};
^~~~
test.cpp:8:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion
from 'int' to 'const bar' for 1st argument
struct bar {
^
test.cpp:8:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion
from 'int' to 'bar' for 1st argument
struct bar {
^
test.cpp:13:6: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
bar::bar() = default;
^
1 error generated.
[ 0s930 | Jan 27 01:35PM ]
g++也有类似的命运:
steve@steve-pc /tmp> g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:17:12: error: no matching function for call to ‘bar::bar(<brace-enclosed initializer list>)’
bar b{0};
^
test.cpp:8:8: note: candidate: ‘bar::bar()’
struct bar {
^~~
test.cpp:8:8: note: candidate expects 0 arguments, 1 provided
test.cpp:8:8: note: candidate: ‘constexpr bar::bar(const bar&)’
test.cpp:8:8: note: no known conversion for argument 1 from ‘int’ to ‘const bar&’
test.cpp:8:8: note: candidate: ‘constexpr bar::bar(bar&&)’
test.cpp:8:8: note: no known conversion for argument 1 from ‘int’ to ‘bar&&’
[ 0s718 | Jan 27 01:35PM ]
这意味着它实际上是直接初始化bar b(0)
,而不是聚合初始化。
这可能是因为如果您没有提供显式构造函数实现,这可能会成为一个外部符号,例如:
bar::bar() {
this.b = 1337; // whoa
}
编译器在非优化阶段无法将其推断为无操作/内联调用。
bar
的构造函数是用户自定义的,而foo
的构造函数是默认的。 - Jarod42main()
中可见bar::bar()
的定义——它可能是在单独的编译单元中定义并且执行非常不平凡的操作,而在main()
中只有声明是可见的。我认为您会同意,这种行为不应因将bar::bar()
的定义放在单独的编译单元中与否而改变(即使整个情况令人费解)。 - Max Langhofint a = 0;
。 - NathanOliver