#pragma pack(1)导致分段错误

问题描述 投票:0回答:1

在某个时候,我的大项目的代码开始出现这样的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)
  • g ++
  • 选项中删除-Ofast
  • 删除for(;;){(将std::cout ...移出循环)
  • #include <iostream>之前移动#pragma pack(1)
  • 尝试使用g ++ 7.4.0

g ++ 9.2.1构建(结果相同)。

在某个时候,我的大项目的代码开始通过以下堆栈跟踪获取分段错误运行时错误:0#std :: basic_ios> :: widen(__c = 10'\ n',this =)在/ usr / include / c ++ / 7 /bits/basic_ios.h:...

c++ g++ cout pragma pragma-pack
1个回答
0
投票
#pragma pack(1)
// ...
#include <iostream>
© www.soinside.com 2019 - 2024. All rights reserved.