Describe
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.
C++ Code
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.
Finally find the bug
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?
more about this
The same code with the #pragma pack(1)
could run on the arm but failed on x86 platform as discussed above.
- OS Linux wang-X556UQK 5.4.0-54-generic #60-Ubuntu SMP Fri Nov 6 10:37:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
- ubuntu 20.04 release
- gcc Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
- eigen 3.3.7
- OS Linux iTOP4412-ubuntu-desktop 3.0.15 #20 SMP PREEMPT Tue Mar 31 22:03:51 CST 2020 armv7l armv7l armv7l GNU/Linux
- ubuntu 12.04 customed by vender Linaro 12.04 (GNU/Linux 3.0.15 armv7l)
- gcc Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
- eigen 3.0.5