Reputation: 2226
At some point my big project's code started getting segmentation fault runtime errors with such stacktrace:
0# std::basic_ios >::widen (__c=10 '\n', this=) at /usr/include/c++/7/bits/basic_ios.h:450
1# std::endl > (__os=...) at /usr/include/c++/7/ostream:591
2# std::ostream::operator<< (__pf=, this=) at /usr/include/c++/7/ostream:113
3# main () at segfault.cpp:11
where last (3#) was always pointing to std::cout lines like std::cout << "hello" << std::endl;
So I brought down my code to this minimal construct which still causes the same error:
#pragma pack(1)
struct Point {
int x;
};
#include <iostream>
int main()
{
for(;;){
std::cout << "hello" << std::endl;
}
}
Which is built with g++ -std=c++17 segfault.cpp -o segfault -g -Ofast
command.
Doing any of the following cancels the error:
#pragma pack(1)
-Ofast
from g++ optionsfor(;;){
(moving std::cout ...
out of the loop)#include <iostream>
before #pragma pack(1)
Tried building with g++ 7.4.0 and g++ 9.2.1 (same results).
Upvotes: 0
Views: 723
Reputation: 238351
#pragma pack(1) // ... #include <iostream>
You've applied #pragma pack to the declarations in the standard library header(s) that you include. The standard library that your executable links to at runtime probably did not apply the pragma. Your executable is not compatible with the runtime library that is uses.
Upvotes: 3