user3314899
user3314899

Reputation: 53

Checking for a memory leak?

For my code, expand is to double the capacity of the vector. It should dynamically reallocate memory for the dynamically allocated array and update the value of capacity while not creating a memory leak.

I was wondering how you would check for a memory leak as my testing doesn't show execution times in Visual Studio.

void IntVector::expand(){
    cap = cap * 2;
    int *data2;
    data2 = data;
    IntVector::~IntVector();
    data = new int[cap];
    data = data2;
    delete data2;
}

header (I understand that you aren't supposed to be using namespace std).

#ifndef INTVECTOR_H
#define INTVECTOR_H

using namespace std;
class IntVector{
private:
    unsigned sz;
    unsigned cap;
    int *data;
public:
    IntVector();
    IntVector(unsigned size);
    IntVector(unsigned size, int value);
    unsigned size() const;
    unsigned capacity() const;
    bool empty() const;
    const int & at (unsigned index) const;
    const int & front() const;
    const int & back() const;
    ~IntVector();
    void expand();

};

#endif

main file

#include "IntVector.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;



IntVector::IntVector(){
    sz = 0;
    cap = 0;
    data = NULL;
}

IntVector::IntVector(unsigned size){
    sz = size;
    cap = size;
    data = new int[sz];
    *data = 0;
}

IntVector::IntVector(unsigned size, int value){
    sz = size;
    cap = size;
    data = new int[sz];
    for(int i = 0; i < sz; i++){
        data[i] = value;
    }
}

unsigned IntVector::size() const{
    return sz;
}

unsigned IntVector::capacity() const{
    return cap;
}

bool IntVector::empty() const{
    if(sz > 0){
        return false;
    }
    else{
        return true;
    }
}

const int &IntVector::at(unsigned index) const{
    if(index > sz){
        exit(1);
    }
    else{
        return data[index];
    }
}

const int &IntVector::front() const{
    return data[0];
}

const int &IntVector::back() const{
    return data[sz];
}

IntVector::~IntVector(){
    delete data;
}

void IntVector::expand(){
    cap = cap * 2;
    int *data2;
    data2 = data;
    IntVector::~IntVector();
    data = new int[cap];
    data = data2;
    delete data2;
}

Edit::

void IntVector::expand(){
    cap = cap * 2;
    int *data2 = data;
    data = new int[cap];
    delete[] data2;
    delete data2;
}

Upvotes: 0

Views: 162

Answers (3)

Omar Himada
Omar Himada

Reputation: 2588

To test for memory leaks in Visual Studio:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

and this next line will automatically display a memory-leak report at every place in your code where an application exit occurs.

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

Edited for maxywb

Upvotes: 1

Demian Kasier
Demian Kasier

Reputation: 2533

I use task manager from windows.

in windows 8: Details tab -> right click on a column and 'select columns' -> GDI objects

Keep an eye on this column, if this keeps rising while it shouldn't then you have a leak.

Upvotes: -1

Leeor
Leeor

Reputation: 19736

These 2 lines:

data = new int[cap];
data = data2;

Allocate an array of ints, and then immediately override the pointer leading to it, thereby losing that allocated memory for ever. That's a memory leak.

Using valgrind or similar tools should lead to these errors very easily.

Upvotes: 3

Related Questions