0

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.

x86
  • 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
arm
  • 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
  • 1
    Works for me https://godbolt.org/z/n7zP7T with address sanitizer enabled. Please provide [mcve]. Make sure you have rebuilt project properly. – Marek R Nov 24 '20 at 13:41
  • Thanks, I edit my problem. And I try to run the tiny code in a separate cpp file, it works fine. But it meets segmentation fault in my project and I don't know why the cout is wrong. – Harry Potter Nov 25 '20 at 05:02
  • I'm guessing that somewhere in your project there is a global variable/constant which initialization contains Undefined Behavior. MSVC 2019 has experimental address sanitizer, so try enable that to locate actual source of problem. You can also try some static code analysis tool. With current information it is impossible to help you. You can also try with other compiler clang/gcc address sanitizer to locate problem. – Marek R Nov 25 '20 at 10:16
  • If the "Restart from scratch" approach to produce a [mre] does not work, try the "divide and conquer" approach: Keep removing `#include` lines, and if that is not possible anymore, start removing lines from the include files themselves. Make sure to post a self-contained example (as the last step you need to copy the contents of all non-standard headers into your example). – chtz Nov 25 '20 at 16:18
  • At the moment, your first code snippet won't even compile, since you removed the `using Eigen::Matrix3d;` -- unless some of the headers also had that line (or even worse, had a conflicting class/typedef with the same name ...) – chtz Nov 25 '20 at 16:23
  • Hi, guys, I finally find the bug. I write such a code in an include file ```c++ #pragma pack(1) ``` But I don't know why this make such a serious error, I delete the code and all is right. – Harry Potter Nov 26 '20 at 10:54
  • 1
    @HarryPotter You are not supposed to leak packing overrides outside your private headers. See for example [pragma pack(push) without corresponding pop leads to stack smashing](https://stackoverflow.com/questions/58197050/pragma-packpush-without-corresponding-pop-leads-to-stack-smashing). – dxiv Nov 28 '20 at 06:09
  • Thanks, I learned the ```#pargma pack ```, and now I know how to solve the errors. – Harry Potter Nov 29 '20 at 13:37
  • This got cross-posted as Eigen issue: https://gitlab.com/libeigen/eigen/-/issues/2074 (and rejected) – chtz Nov 30 '20 at 16:30

1 Answers1

0

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.