From the same site Moschops linked earlier:
http://people.ds.cam.ac.uk/nmm1/C++/Exercises/index.html
In particular:
they are not always written in a good style, and
I did most of them in some chapters but only a few in others (though I did all that looked as if they needed something new), and
they rarely include the error checking and commenting that the book asks for, and
they haven't been properly tested, and some are deliberately incorrect (to check on failure modes), and
they don't all do the drill or exercise exactly, because I fairly often got bored :-)
Don't do as these examples do - do as the book says! |
I would say the examples are
quite different to what Bjarne would have written :+) The page was updated last October - so it's not an old web page, it's shame such material is being used to teach with.
For a start, one shouldn't be using
new
or
delete
, or use raw pointers at all. There are pointers to vectors because new was used! STL containers store their data on the heap implicitly. I am guessing that is why the jill vector shouldn't be deleted because it's an STL container. Normally they are let to go out of scope naturally (implicitly deleted), but here they are artificially kept alive by returning a pointer from the function.
Trivially, the functions create brand new arrays (as in a different one with the same data) each time they are called.
I am uneasy about delete being used from a different scope. I can imagine it being used at a low level within in a class say, but using it on something created with another general function is rather smelly IMO.
Anyway, it's all messy.
In my mind the whole idea of the STL: is to use it's containers, functions and algorithms and features. Then one doesn't have to worry about memory management at all. The STL is supposed to make life easier and convenient.
Here is a guideline written by Bjarne Stroustrup & Herb Sutter:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html#Rr-newdelete