Chapter 16. Writing Exception-Safe Code—Part 9

Difficulty: 8

How well do you understand the innocuous expression delete[] p? What are its implications when it comes to exception safety?

And now, for the topic you've been waiting for: “Destructors That Throw and Why They're Evil.”

Consider the expression delete[] p;, where p points to a valid array on the free store, which was properly allocated and initialized using new[].

  1. What does delete[] p; really do?

  2. How safe is it? Be as specific as possible.

Solution

Solution

This brings us to a key topic, namely the innocent looking delete[] p;. What does it really do? And how safe is it?

Destructors That Throw and Why They're ...

Get Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.