Skip to Content
Mastering C++ Programming
book

Mastering C++ Programming

by Jeganathan Swaminathan
September 2017
Beginner to intermediate
384 pages
8h 4m
English
Packt Publishing
Content preview from Mastering C++ Programming

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Multi-Paradigm Programming with Modern C++

Multi-Paradigm Programming with Modern C++

Georgy Pashkov

Publisher Resources

ISBN: 9781786461629Supplemental Content