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 uninitialized memory access

The following example code demonstrates the use of uninitialized memory access and how the same can be detected using Memcheck:

#include <iostream>using namespace std;class MyClass {    private:       int x;    public:      MyClass( );  void print( );}; MyClass::MyClass() {    cout << "\nMyClass constructor ..." << endl;}void MyClass::print( ) {     cout << "\nValue of x is " << x << endl;}int main ( ) {    MyClass obj;    obj.print();    return 0;}

Let's now compile and detect the uninitialized memory access issue using Memcheck:

g++ main.cpp -gvalgrind ./a.out --track-origins=yes==51504== Memcheck, a memory error detector==51504== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.==51504== Using Valgrind-3.11.0 and LibVEX; ...

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