I have a large C++ software project build with gcc. After adding some new code into the repository, one of the binary started to get Segmentation Fault on larger test data (no segfault on a few smaller test inputs). I have tested on several different hardwares with two different systems. Ubuntu 18 and Centos6. GCC compiler tested are 6.3, 7.4 and 9.2. Binary build on Ubuntu always good and on Centos is always bad (segfault). The OS giving trouble:
uname -a
Linux hostname 3.10.0-957.27.2.el7.x86_64 #1 SMP Mon Jul 29 17:46:05 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
My Unbuntu has single compiler, but the Cenos usually has a old gcc 4.8, and on top of that we build 6.3, 7.4, or 9.2. My C++ code was build with various versions gcc and on different harwares, all got segfault at
std::ostream& operator(ostream&, const string&);
but from one of several random places in my code. I used the -O2 or -O3 option. When using -O0 option the binary has no problem but a few times slower. I am trying to generate production code which needs to be faster.
I know one solution is to find out which line of code is causing this segmentation fault, which I will try next. My question is that is there any option while making the gcc compiler that may help to resolve this segfault issue?
I have followed suggestion (provide in the comment section below) turning on full warning and updated all dependent libraries with warning message too (include boost). It looks this solved the problem. I learn a big lesson: any code we write has to pass pedantic Wall. Someone should write an answer to help other programmers.