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 outside the boundary of an array

The following example demonstrates memory access outside the boundary of an array:

#include <iostream>using namespace std;int main ( ) {    int a[10];    a[10] = 100;    cout << a[10] << endl;    return 0;}

The following output shows the valgrind debug session that precisely points to memory access outside the boundary of the array:

g++ arrayboundsoverrun.cpp -g -std=c++17 jegan@ubuntu  ~/MasteringC++/Debugging  valgrind --track-origins=yes --read-var-info=yes ./a.out==28576== Memcheck, a memory error detector==28576== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.==28576== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info==28576== Command: ./a.out==28576== 100

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