Thursday 30 September 2021

Chapter 21 // Exercise 1 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Exercise 1

Go through the chapter and do all Try This exercises that you haven't already done.

Try This Pg 761

Github: 

To see if they were truly logically different, I went into debug mode and then compared the disassembly created.

You can see that Visual Studio produced identical code, however that is in debug mode. In release mode, it was slightly different:
The first implementation is faster and could be more optimised due to the NOP instruction which I believe aligns the loop in "jump chunks" (very brief explanation because I don't fully understand yet) which can help with instruction fetches. Both loops are doing basically the same thing though:
  • setting the conditional jump
  • comparing the contents of the iterator with 4
  • jumping back to the top while the condition is not equal and increasing the iterator
Try This Pg 765

Github: N/A

1. The value could be changed without you knowing, potentially breaking a part of the code.
2. Someone could change the predicate to use a different name value.
3. Someone could try an make the value global to access it in other places; or start using it where they're not supposed to.
I'd hate to see it in any application.

Try This Pg 774

Github: 

.

Try This Pg 785

Github: 

This one confused me a bit as on pg 783 he puts {"AA"] = "Alcoa Inc."} in the constructor of the map dow_name. This wouldn't compile in VS2019 and gave errors due to the '] ='. 

Also, the code didn't run initially when using inner_product() given on pg784. It asserted with the error "cannot dereference end map/set iterator". Turns out that Bjarne is a sneaky fox. The lines:
double alcoaprice = dowPrice["AAA"];
double bowingPrice = dowPrice["BA"];
ADD to the map if those keys are not found so my dowPrice map became a size of 5 whereas dowWeight was still 3. This caused the dereferencing error on the map iterator. I will not forget ever again that map will default add an element if it doesn't exist.

Try This Pg 787

Github: 

I'm pretty sure I'm running with at least C++17 so this worked. The order was different.

Try This Pg 792

Github: 

I set max_size to INT_MAX and that still caused an error. For this the overflow caused an exception so it was quite bad and I would not be tempted to ship it.


No comments:

Post a Comment