Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Practice this question and more.


Why is it recommended to set a pointer to zero after deleting it?

  1. To improve memory efficiency

  2. To avoid deleting it twice

  3. To prepare it for garbage collection

  4. It's not recommended

The correct answer is: To avoid deleting it twice

After deleting a pointer, its value will be just a memory address, but it will still be pointing to that location. This means that if the pointer is dereferenced again, it will access the same location in memory that was previously deleted. This can result in unexpected behavior or even program crashes. By setting the pointer to zero, it ensures that the pointer is no longer pointing to a potentially deleted memory location, avoiding any issues that may arise from attempting to use a deleted pointer. Options A, C, and D are incorrect because they do not address the specific issue of avoiding dereferencing a deleted pointer. Improving memory efficiency (option A) is a general goal but not the primary reason for setting a pointer to zero after deleting it. Preparing for garbage collection (option C) is only relevant in languages with automatic memory management, and even then, it is not the primary reason for