Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Tools for Practical <strong>C++</strong> Development 19<br />
==2938== by 0x41A5DE6: strtol (strtol.c:110)<br />
==2938== by 0x41A3160: atoi (atoi.c:28)<br />
==2938== by 0x80486B9: main (reciprocal.cc:9)<br />
==2938== Address 0x0 is not stack’d, malloc’d or (recently) free’d<br />
==2938== ...<br />
==2938==<br />
==2938== HEAP SUMMARY:<br />
==2938== in use at exit: 0 bytes in 0 blocks<br />
==2938== total heap usage: 0 allocs, 0 frees, 0 bytes allocated<br />
==2938==<br />
==2938== All heap blocks were freed -- no leaks are possible<br />
==2938==<br />
==2938== For counts of detected and suppressed errors, rerun with: -v<br />
==2938== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 6)<br />
[1] 2938 segmentation fault valgrind --leak-check=yes ./reciprocal<br />
When the error occurs, you get an error message and a stack trace (and a lot of other information).<br />
In this case, it is almost the same information as when you used gdb, but this is only because the<br />
error resulted in a segmentation violation. But most errors don’t result in immediate traps:<br />
void zero(int* v, size_t n) {<br />
for (size_t i = 0; i < n; ++i)<br />
v[i] = 0;<br />
}<br />
int main() {<br />
int* v = new int[5];<br />
zero(v, 10); // oops, should have been 5<br />
// ... this error may result in a trap now or much later, or wrong results<br />
}<br />
A6. The file vgtest.cc contains a test program with two functions with common programming<br />
errors. Write more functions to check other common errors. Note that valgrind cannot find<br />
addressing errors in stack-allocated arrays (like int v[10]; v[10] = 0). Run the program<br />
under valgrind, try to interpret the output.<br />
Advice: always use valgrind to check your programs! Note that valgrind may give<br />
“false” error reports on <strong>C++</strong> programs that use STL or <strong>C++</strong> strings. See the valgrind FAQ,<br />
http://valgrind.org/docs/manual/faq.html#faq.reports<br />
4 Makefiles and Topological Sorting<br />
4.1 Introduction<br />
Targets and prerequisites of a makefile form a graph where the vertices are files and an arc<br />
between two vertices corresponds to a dependency. When make is invoked, it computes an order<br />
in which the files are to be compiled so the dependencies are satisfied. This corresponds to a<br />
topological sort of the graph.<br />
Consider a simplified makefile containing only implicit rules, i.e., lines reading target:<br />
prerequisites. You are to implement a <strong>C++</strong> program topsort that reads such a file, whose name<br />
is given on the command line, and outputs the targets to standard output in an order where<br />
the dependencies are satisfied. If the makefile contains syntax errors or cyclic dependencies, the<br />
program should exit with an error message.<br />
This is not a trivial task. The solution will be developed stepwise in the following sections.