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

Get Mastering C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.