O'Reilly logo

Mastering C++ Programming by Jeganathan Swaminathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Detecting memory access to already released memory locations

The following example code demonstrates memory access to the already released memory locations:

#include <iostream>using namespace std;int main( ) {    int *ptr = new int();    *ptr = 100;    cout << "\nValue stored at pointer location is " << *ptr << endl;    delete ptr;    *ptr = 200;    return 0;}

Let's compile the preceding program and learn how Valgrind reports the illegal memory access that attempts to access an already released memory location:

==118316== Memcheck, a memory error detector==118316== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.==118316== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info==118316== Command: ./a.out==118316== Value stored at ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required