在某个时候,我的大项目的代码开始出现这样的stacktrace的分段错误运行时错误:
行,例如0#std :: basic_ios> :: widen(__c = 10'\ n',this =)在/usr/include/c++/7/bits/basic_ios.h:4501#std :: endl>(__os = ...)在/ usr / include / c ++ / 7 / ostream:5912#std :: ostream :: operator <
最后一个(3#)总是指向std :: cout
std::cout << "hello" << std::endl;
所以我将代码简化为仍然会导致相同错误的最小结构:
#pragma pack(1) struct Point { int x; }; #include <iostream> int main() { for(;;){ std::cout << "hello" << std::endl; } }
使用
g++ -std=c++17 segfault.cpp -o segfault -g -Ofast
命令构建。
执行以下任何
会消除该错误:#pragma pack(1)
-Ofast
for(;;){
(将std::cout ...
移出循环)#include <iostream>
之前移动#pragma pack(1)
尝试使用g ++ 7.4.0
在某个时候,我的大项目的代码开始通过以下堆栈跟踪获取分段错误运行时错误:0#std :: basic_ios> :: widen(__c = 10'\ n',this =)在/ usr / include / c ++ / 7 /bits/basic_ios.h:...
#pragma pack(1) // ... #include <iostream>