debug in C++

Hello all thanks for adding me to c++ forum.
am newbee to c++ programming I would like to know if der is a command to debug line by line in linux. ex: if i compile the below code g++ -xyz prog.cpp /S
it should compile single line and show the o/p each time i press S.
Please guide me.
Thanks!!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>
char * buildstr(char c, int n); // prototype
int main()
{
using namespace std;
int times;
char ch;
cout << “Enter a character: “;
cin >> ch;
cout << “Enter an integer: “;
cin >> times;
char *ps = buildstr(ch, times);
cout << ps << endl;
delete [] ps; // free memory
ps = buildstr(‘+’, 20); // reuse pointer
cout << ps << “-DONE-” << ps << endl;
delete [] ps; // free memory
return 0;
}
// builds string made of n c characters
char * buildstr(char c, int n)
{
char * pstr = new char[n + 1];
pstr[n] = ‘\0’; // terminate string
while (n-- > 0)
pstr[n] = c; // fill rest of string
return pstr;
}
Last edited on
You can use gdb. Compile the file with the -g flag and don't turn on optimizations.

g++ -o prog prog.cpp -g
This will compile the program into an executable file named prog.

Run gdb ./prog to start debug prog using gdb. You then write commands to instruct gdb what to do. First you need to start the program by typing start and press enter. Use next to step to the next line. If you want to step into functions you can use step instead. To print local variables you can do info locals. To print the value of a specific variable type print NameOfVariable. To quit gdb you type q.

Note that you can quickly repeat previous commands using the up key and auto complete using the tab key (as in the normal terminal) so you don't have to repeatedly type next, next, ...

If you want to step more than one line at once you can add the number of lines after next or step (e.g. next 5).
Last edited on
Thanks peter!!!It was very help full.
is der a way to look at the address of the variable i tried "info address i" but is not showin any addrress.

print &i
Topic archived. No new replies allowed.