Looking ahead to smart pointers for safety
We have seen many uses of pointers to add flexibility and efficiency to our programs. However, we have also seen that with the power that pointers can provide comes potential havoc! Dereferencing uninitialized pointers can take us to non-existent memory locations that will inevitably crash our programs. Accidentally dereferencing memory that we have marked for deletion is similarly destructive – the memory address may have already been reused by the heap management facility elsewhere in our program. Neglecting to delete dynamically allocated memory when we are done with it will cause memory leaks. Even more challenging is allocating memory in one scope and expecting to remember to delete that memory in another scope. Or, consider what happens when two or more pointers point to the same piece of heap memory. Which pointer is responsible for deleting the memory? This is an issue we will see several times throughout the book with various...