A caution for users at Chapter 5, Ex. 03
When doing the Try Out for Chapter 5, Ex 3 (Pass by Pointer), I ran into a problem that the prior chapters had not quite prepared me for.
When compiling and running the book's code, you're supposed to end up with results like this:
-----------------
Address Passed = 0012FF60
Address received = 0012FF60
incr10 (pnum) = 13
num = 13
----------------------------------
However, when I ran *my* code, I ended up with this:
-----------------
Address received = 0012FF60
Address Passed = 0012FF60
incr10 (pnum) = 13
num = 3
----------------------------------
I seemed to have two problems, the "Address received" and "Address Passed" lines were reversed, and the "num" variable was showing up as 3, when it should have been 13.
I triple checked my code, and substantially, it was exactly as it should be. My function prototype was correct, the function itself was correct, I couldn't figure out what the problem was.
I ran the debugger and saw that after the function call, "num" was in fact 13 - exactly as it should be. And yet, it was still being output as 3.
Eventually, I intuited what was causing the problem.
The book code specified the output statements as this:
-----------------
cout << endl
<< "Address Passed = " << pnum;
cout << endl
<< "incr10 (pnum) = " << incr10 (pnum);
cout << endl
<< "num = " << num;
----------------------------------
However, when I typed it in, I thought I would be more efficient by typing it in like this:
-----------------
cout << endl
<< "Address Passed = " << pnum
<< endl
<< "incr10 (pnum) = " << incr10 (pnum)
<< endl
<< "num = " << num;
----------------------------------
Apparently, having all of this in a single statement caused an interesting effect insofar as the order things were processed and output.
While the book went into some detail in earlier chapters to describe the order in which things are processed by the compiler, I was not prepared for this. Nothing indicated that a simple output statement would also have to be carefully crafted in order to avoid problems.
The way I understood it was that the program (in my original faulty version) would run like this:
1. Output new line
2. Output "Address Passed = "
3. Output value of "pnum"
4. Output new line
5. Output "incr10 (pnum) = "
6. Calculate "incr10 (pnum)"
7. Output result
8. Output new line
9. Output "num = "
10. Output value of "num"
Apparently, I was mistaken. :)
So, my posting this message is for three reasons:
1. Users teaching themselves using this book should be aware of this issue, and hopefully, this post will help you avoid a lot of head-scratching frustration as you try to figure out what went wrong when everything looks like it should work fine.
2. The author may wish to take this into account for the next revision/edition, so that he can address this issue in the book before readers encounter it the hard way.
3. Can someone clarify with some detail exactly what is happening with the faulty version of my code, and how things are processed in an output statement? I want to better understand how I need to code them in the future.
Thanks! And I hope this helps someone! :)
|