-1

I have to debug a segfault in my C++ program using Code::Blocks. Unfortunately, the stack trace isn't showing correctly, instead I see ?? ()

Here is a minimal example:

#include <iostream>

using namespace std;

int main()
{
    int *plop;
    cout << *plop << endl;
    return 0;
}

The debugger says:

Program received signal SIGSEGV, Segmentation fault. In ?? () ()

But I was expecting something more useful like "In main ()"


EDIT: here is the build log, if it helps

-------------- Build: Debug in tests (compiler: GNU GCC Compiler)---------------

g++.exe -Wall -fexceptions -g -O -pedantic -Wextra -std=c++0x -std=c++14  -c D:\C\tests\main.cpp -o obj\Debug\main.o
D:\C\tests\main.cpp: In function 'int main()':
D:\C\tests\main.cpp:8:14: warning: 'plop' is used uninitialized in this function [-Wuninitialized]
     cout << *plop << endl;
              ^
g++.exe  -o bin\tests.exe obj\Debug\main.o  -s  
Output file is bin\tests.exe with size 542.00 KB

2nd EDIT: finally solved :)

For those who came here by google : strip symbols -s and Optimizer -O compiler options were checked in my case, theses options conflicts with -g as they removes debug symbols in compiled code.

Thanks for everyone for answering

12
  • 1
    os, compiler (gcc/mingw), compiler version. etc. also stackoverflow.com/questions/5870038/… and why ?? here > stackoverflow.com/questions/8437526/… Commented Jul 2, 2016 at 18:55
  • As some of you stated, the problem is about the debugger and not about the posted code. Commented Jul 2, 2016 at 19:38
  • About Config: Windows 7; Code::blocks 16.01; Mingw w/ gcc 4.9.2; Project build target is in debug mode Commented Jul 2, 2016 at 19:40
  • I've updated Jesper's answer. (pastebin.com/KHwimTtR) Commented Jul 2, 2016 at 19:52
  • I tried to update it, kek, but you get the point, I hope Commented Jul 2, 2016 at 20:07

2 Answers 2

0

you must initialize the int *plop; pointer like bellow, then print the value :

#include <iostream>

using namespace std;

int main()
{
    int *plop = new int(15);
    // *plop = 120; // you can change the plop value as custom
    cout << *plop << endl;
    return 0;
}

result will be : 15

Sign up to request clarification or add additional context in comments.

3 Comments

Yes of course, but why doesn't the debugger show the correct method name. That's the question
@ThomasWeller I have use of Qt and in thats compiler does not show correct method name, just show signal SIGSEGV.
@Thomas Weller probably because it cannot since the compiler exploited its licence to kill due to encountering UB and generated something unexpected. Only investigating the generated object code/asm will tell.
-1

You are dereferencing an uninitialized pointer. That's undefined behaviour and your program is meaningless.

The compiler is free to generate whatever it pleases (including code causing a segfault or not doing anything at all) - basically, all bets are off and you can trust nothing and you can't even count on your debugger showing you anything sane since it just has to work with what the compiler generated - which could be whatever. Don't work with uninitialized variables / invoke UB.

3 Comments

Currious as to the downvote. What exactely do you take issue with with my answer?
The question is not what's wrong with the code but why the debugger shows no method name
How can the debugger reliably show a method name? It is completely at the mercy of what the compiler generated. And since the code contains UB the compiler is well within its right to generate garbage that the debugger cannot use to produce sensible output. That's just a fact of life you have to learn to deal with and then try to not invoke UB in your code. What else is there to say? And I don't see how that fact makes my answer (pointing out the fact) deserve a downvote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.