Setting Breakpoints

Debugging programs is unavoidable and is much easier if you can make execution stop at any given line in a program, to examine (that is, to inspect) the values of variables. You can do this by setting a breakpoint. For example, say you want to know why the following program (main.cpp) isn't doing what it's supposed to do (note that the line numbers are just for reference):

1 #include<iostream>
2 #include<cstdlib>
3 using namespace std;
4 int main(int argc, char *argv[]) {
5   int count = atoi(argv[0]);
6   for (int i = 0; i < count; i++)
7      cout << argv[1] << endl;
8 }

The program should print out a number of copies of its second argument. You could put in a number of output statements to display the state of the variables, and ...

Get C++ By Example: UnderC Learning Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.