In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.
Chapter 25 // Exercise 1
If you haven't already, do the Try this exercises in this chapter.
Pg 939 - Complete the program above and print out the addresses and sizes of the objects created to see if and how "holes" appear on your machine. If you have time, you might draw memory layouts likes the ones above to better visualise what's going on.
I couldn't get this one to work, or to visualise what he was showing in the book. When a message was deleted, a new node wouldn't take it's place in that freed up memory; it would go somewhere else. Sometimes it did though.
Pg 959 - Get the bits example to work and try out a few values to develop a feel for binary and hexadecimal representations. If you get confused about the representation of negative values, just try again after reading section 25.5.3.
The bits example wouldn't compile due to a narrowing conversion error. I had to add a static cast to the bitset constructor.
Pg 963 - The following example may look innocent, but it is an infinite loop:
void infinite()
{
unsigned char mx = 160; // very large
for(signed char i = 0; i < max; ++i) cout << int(i) << '\n';
}
Run it and explain why.
void infinite()
{
unsigned char mx = 160; // very large
for(signed char i = 0; i < max; ++i) cout << int(i) << '\n';
}
Run it and explain why.
A signed char can be in the range of -127 to 127. An unsigned char can be 0 - 255. The signed char therefore will never reach 160 and it will wrap round causing the infinite loop.
This one was interesting. I was expecting the signed chars to print -127 but they did -128 instead.
Pg 973 - The key was bs; what was the text?
This one took me a while as the code in the book enciphers and I kept wondering why my text file was filled with hex afterwards; turns out I should read the code more carefully.
No comments:
Post a Comment