In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.
Chapter 20 // Exercise 4
Find and fix the errors in the Jack-and-Jill example from section 20.3.1 by using STL techniques throughout.
I didn't see anything completely serious. High() can return a pointer to garbage if the containers passed in are empty. To combat this getFromJack() and getFromJill() check to see if the file is empty. If it, it returns a nullptr. That way we're not doing any unnecessary memory allocations and we can gracefully handle checks for nullptrs without crashing the program with errors. Having a nullptr is perfectly fine so I didn't want to error if the file was empty.
I also modified getFromJill() to be able to handle any size to prevent unwanted allocations. getFromJack() is a bit more difficult as array size must be specified at compile time. A way to get round that is to read jack's data into a vector first then create the array from that size but that kind of defeats the point of using an array. I'm going to leave it the way it is for getFromJack() as arrays should be used when you know the size.
EDIT 14/06/2021
I showed this exercise to one of our senior engine programmers who pointed out that there is no handling for if the pointers supplied to high() are the correct way round. He said this could cause a near infinite loop. To be honest it never occurred to me to pass in the pointers the wrong way round, why would someone do that? I guess that's why I'm not a QA tester.
No comments:
Post a Comment