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. ...