Using memory after deleting it

To further demonstrate how the address sanitizer works, let's look at the following example:

int main(void){    auto p = new int;    delete p;    *p = 0;}

When we execute this, we see the following:

The preceding example allocates an integer and then deletes the integer. We then attempt to use the previously deleted memory. Since this memory location was originally allocated, ASAN has the address cached. When the dereference to the previously deleted memory occurs, ASAN is capable of detecting the issue as a heap-use-after-free error. It is only capable of detecting this issue because the memory was previously allocated. ...

Get Advanced C++ Programming Cookbook 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.