I'm writing a toy code with Eigen. But when I try to print the matrix I, unfortunately, meet the segment fault when cout. The following first code is my project code that meets the segmentation fault. I know that if I just write a tiny code as the second code, it works fine. So I don't know what's the matter with my project code for that it failed in the cout as the pictures show.
The most minimum test code is https://godbolt.org/z/GePx5b
#pragma pack(1)
#include <iostream>
#include <Eigen/Dense>
#include <string>
#include <fstream>
#define BUFFER_SIZE 256
#define WORK_SPACE "PositioningSystem/"
using Eigen::Matrix3d;
int main(int argc, char const *argv[]) {
Eigen::Matrix3d m;
m << 1, 2, 3,
4, 5, 6,
7, 8, 9;
std::cout << m << std::endl;
return 0;
}
If you write the first code #pragma pack(1)
, you will meet segmentation fault, and if you delete it, all work fine.
In an include file, I write such a code as the following
#pragma pack(1) //指示struct不进行内存对齐
When I delete this code, all is right. Anyone has any thoughts about this bug?
The same code with the #pragma pack(1)
could run on the arm but failed on x86 platform as discussed above.
Yes, I finally successfully overcome the issue. It's the wrong with the #pragma pack
. So, please use the sentence following the #pragma pop
, just like the new
and delete
must be the same to make sure the program is right.