Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB console can't show the result of printf

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.

like image 881
Yuan Wen Avatar asked Feb 06 '23 06:02

Yuan Wen


1 Answers

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.

like image 117
Toby Speight Avatar answered Feb 18 '23 22:02

Toby Speight