While moving to a newer version of CC compiler, a segfault manifested in a module that used to work.
From the core file I could learn in what function the segfault originated. When I observed the function I could not find anything suspicious.
First major problem was that the segfault reproduced only when compiling in "release" (optimizations turned on), and didn't reproduce on "debug". Also, the segfault doesn't reproduce on g++.
Now I started to use printing, and a bigger problem arose- when adding cout
/printf
(to binary search the segfault line/print the value of pointers) to certain lines in the code, the segfault didn't reproduce. Moreover, I added a cout in a certain line in the code that maintained the segfault, which supposedly means that the segfault happens before that line. Commenting lines after that line made the segfault go away.
To me, this screams memory corruption (specifically of the stack), but I have no idea on how to advance on this without looking at the generated assembly.
Any ideas? thanks in advance.
I'm working on SunOS_5.10_Studio_12_5.12_64, CC version "Sun C++ 5.12 SunOS_sparc 2011/11/16"
A common run-time error for C programs by beginners is a "segmentation violation" or "segmentation fault." When you run your program and the system reports a "segmentation violation," it means your program has attempted to access an area of memory that it is not allowed to access.
Regarding Best practices to avoid segmentation faults, testing the code with tools like Valgrind/Efence helps in catching memory over runs. Besides using tools, writing and organising code carefully helps to great extent. Hope this helps.
Most memory errors which aren't memory leaks end up resulting in a segmentation fault. A segmentation fault is raised when the operating system realizes that your program is trying to access memory that it shouldn't have access to.
You should use a memory debugger/profiler like valgrind. It will quickly tell you the location of corruption. On Solaris you can try libumem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With