7

I'm using gdb in RedHat to debug C++ code. To better debug my code, I added printf("XXX").

However, after the execution of printf("XXX"), the gdb console didn't show XXX.

Other parts of my code works fine.

5
  • If you execute the program without debugger do you see the XXX on the console? Commented Aug 30, 2016 at 10:40
  • It seems to be no.@Hayt Commented Aug 30, 2016 at 10:47
  • 2
    Seams to be a buffer issue with GDB: it's append also to me, that at the end of debug, remaining data from stdout buffer isn't flush. Try printf("XXX\n"); Commented Aug 30, 2016 at 10:48
  • so maybe the binary you are debugging is still the old one. Commented Aug 30, 2016 at 10:50
  • You are right, thank you.Why not write a more detailed answer?@Garf365 Commented Aug 30, 2016 at 10:52

1 Answer 1

11

It's likely that your output is line-buffered, and because you didn't end the print with a newline, the output hasn't been flushed. Three easy fixes:

  1. std::printf("XXX"); std::fflush(stdout);
  2. std::printf("XXX\n");
  3. std::puts("XXX");

Also, take care if you're mixing C-style FILE* i/o with C++-style streams.

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

3 Comments

I am curious about why this isn't the same case for Windows? I mean, under Windows, debug with Visual Studio, I get output every time with cout << "hello";, no extra std::endl or std::flush needed. Is it because Visual Studio already taking care of this?
@Rick, probably the library there is using a different buffering strategy. It's not a platform I've had any contact with, so that would be pure speculation on my part. Perhaps worth asking a question, if it's not already addressed by one?
Thank you for replying. I don't wanna bother others by asking this question,after all, it's kinda a useless question. I just feel a bit surprised when I don't find any GDB tutorial mentioning this. I mean, this is very important when debugging. For exmaple, I come from Windows, have no idea what's wrong when I don't see the cout output at first time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.