2

I have an assignment to write a class that dynamically allocates a 2D array, then I have to write functions to work with it. One of the functions is an overload function of operator+. Everything works as expected, even the operator+ functions until the program calls the destructor. The destructor has a simple job of releasing the memory allocated previously. I know the code is correct and I have tested it manually, but when the computer calls this function at the end of the program, I get "Unhandled exception at 0x000566E1 in 2DArray.exe: 0xC0000005: Access violation reading location 0xFEEEFEF2." I tried all sorts of things, I have been sitting at this problem for two days now and I'm exhausted. Its due this Friday. If anyone can try and help, I would appreciate it.

Here are the two functions that I think the problem is buried in. Also, note that when there is no destructor, the program runs completely fine, but I have to make sure the memory gets released before the program quits, its part of the assignment.

 Square_Matrix Square_Matrix::operator+(Square_Matrix & object)
{
    Square_Matrix sum;
    sum.Set_Size(size);
    for (int i = 0; i < size; i++)
    {
        for (int b = 0; b < size; b++)
        {
            sum.Set_Elem((matrix[i][b] + object.Get_Elem(i, b)),i, b);
        }
    }
    return sum;
}

Square_Matrix::~Square_Matrix()
{
    // destructor
    // release the allocated memory

    if (size > 0)
    {
        for (int d = 0; d < size; d++)
        {
            delete[] matrix[d];
        }
        delete[] matrix;

        size = 0;
    }
}
5
  • 1
    Please show the constructor and the copy constructor as well, just in case the problem is in one of those. Commented Feb 19, 2014 at 3:47
  • How is the memory allocated in the constructor? Also, what does set_size() look like? Commented Feb 19, 2014 at 3:57
  • I dont have a copy constructor. could that be the problem? Commented Feb 19, 2014 at 3:57
  • 1
    @glina126: Yes. Googling for something like "C++ rule of three" should get you quite a bit of information. Commented Feb 19, 2014 at 4:03
  • ok, very nice. I will read up on that. Thanks! Commented Feb 19, 2014 at 4:04

2 Answers 2

2

It looks like you need a copy constructor.

Also, your argument to operator+ should be const Square_Matrix &object. You don't have any intention of changing the second object, do you? Tell the compiler. It will appreciate it.

In fact, you don't have any intent to change the first object either, so you should declare it Square_Matrix Square_Matrix::operator+(const Square_Matrix & object) const

The second const tells the compiler to make this a const pointer as well.

Sign up to request clarification or add additional context in comments.

1 Comment

copy constructor was the key here. Also, for some weird reason, my compiler is not happy that I do constants. It highlights objects as syntax error. object.Get_Elem(i, b) gives this error: "const Square_Matrix &object Error: The object has type qualifiers that are not compatible with the member function object type is: const Square_Matrix"
0

When you are deleting all the values in the dynamically allocated array, you are deallocating the entire thing.

for (int d = 0; d < size; d++)
{
    delete[] matrix[d];
}

You have to remove the line

delete[] matrix;

It should stop giving the exception then.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.