2

I'm writing a program which creates 2D Dynamic Array in the constructor and delete it in the destructor. But when I return an object of this class from any function, it causes Runtime Error.

Error: Access violation reading location 0xDDDDDDD1

#include <iostream>
#include <cstdlib>
using namespace std;

class Matrix {
private:
    int **MyArray;
    int row, col;
public:
    Matrix(int r, int c) {
        MyArray = new int*[r];
        for (int i = 0; i < r; i++) {
            MyArray[i] = new int[c];
        }

        row = r;
        col = c;
    }

    Matrix test() {
        Matrix Temp(row, col);
        return Temp;
    }

    ~Matrix() {
        for (int i = 0; i < row; i++) {
            delete[] MyArray[i];
        }
        delete[] MyArray;
    }

};


int main() {
    Matrix m(2, 2), m2(2, 2);
    m.test();

    system("pause");
}

While tracking down the error I come across that after deleting Temp object. The program also deletes another location.

#include <iostream>

using namespace std;

class Matrix {
private:
    int **MyArray;
    int row, col;
    static int count;
public:
    Matrix(int r, int c) {
        MyArray = new int*[r];
        for (int i = 0; i < r; i++)
            MyArray[i] = new int[c];

        row = r;
        col = c;
    }

    Matrix test() {
        Matrix Temp(row, col);
        cout << "Temp->" << &Temp << endl;
        return Temp;
    }

    ~Matrix()
    {
        cout << "deleting-> " << this << endl;
        for (int l = 0; l < row; l++)
            delete[] MyArray[l];
        delete[] MyArray;
    }
};

void main() {
    Matrix m(2, 2), m2(2, 2);
    cout << "m->" << &m << endl;
    cout << "m2->" << &m2 << endl;
    m.test();
}

Tracking Error

Praetorian
  • 106,671
  • 19
  • 240
  • 328
soachishti
  • 341
  • 2
  • 13
  • I don't see any 2d array there – perencia Jun 03 '15 at 06:41
  • MyArray is 2d array. Pointer to pointer... – soachishti Jun 03 '15 at 06:44
  • A pointer to pointer does not imply a 2d array. You have an array of pointers. That is one dimensional. – perencia Jun 03 '15 at 06:47
  • 1
    Don't implement a `Matrix` using an `int**`, write a wrapper around `std::vector` and you won't have to worry about the Rule of Three. If you can't use `vector`, then use an `int*`, allocate an array of size `row*column` and index into it appropriately. – Praetorian Jun 03 '15 at 06:50
  • Actually this is a question of lab exam in which I have to implement multidimensional array using pointer to pointer variable. – soachishti Jun 03 '15 at 06:53
  • Thanks, Adding copy constructor and copy assignment solved the problem :) – soachishti Jun 03 '15 at 07:18
  • 1
    There is also one point to note down. This code does not produce run-time error with g++ compiler as there is so called return value optimization (RVO). RVO causes, temp object to be returned without calling the copy constructor. As far as I know RVO is not available for visual c++ yet, that was why you were getting a run-time error. – yildirim Jun 03 '15 at 09:43
  • @yildirim Thanks for telling about RVO. I was wondering, Why It was working fine on Code::Block. – soachishti Jun 03 '15 at 13:08

0 Answers0