简体   繁体   中英

why am I consistently getting malloc errors when I try to delete my 2D array made using pointers?

I'm trying to delete my 2D array, but I consistently get errors when I try to delete it, we have to work backwards so I delete the elements first, then the column array, then the row array. here is my code for the constructor in my class, MyMatrix:


    private:
    int m; //rows
    int **ptr; //ptr to first dimension
    int n; // columns

    public:

    MyMatrix() //constructor
    {
        m = 0;
        n = 0;
        ptr = new int*[m];
        int *length_arr = new int[m]; 
        for (int i = 0; i <= m-1; i++)
        {
            *(ptr+i) = new int[n];
            *(length_arr+i) = n;
        }
    }

and my destructor looks like this:

 for(int i = 0; i <= m-1; i++)
        {
            for (int j = 0; j <= n-1; j++)
            {
                delete ((*(ptr+i))+j);
            }
            delete[] *(ptr+i);
        }
        delete[] ptr;

the error I'm getting is:

assg7(2677,0x100de3d40) malloc: *** error for object 0x12d606804: pointer being freed was not allocated

I've wracked my brain for where I can fix this, for context, I'm doing an assignment with operator overloading. I specifically need a delete function to work properly for my = assignment overloading since I want to delete and again reallocate memory to equate two matrices, but the terminal is showing malloc errors and is thus not equating the matrices. for additional info here is my = overloading code:

void operator = (const MyMatrix &obj)
    {
        if(n == obj.n && m == obj.m)
        {
         //for loop to equate elements in this-> to the elements of the passed object   
        }
        else
        {
            for(int i = 0; i <= m-1; i++)
            {
                for (int j = 0; j <= n-1; j++)
                {
                    delete ((*(ptr+i))+j);
                }
                delete[] *(ptr+i);
            }
            delete[] ptr;

            // the code for assigning new memory according to the passed objects rows and colums goes here
            //then for loop to equate elements in this-> to the elements of the passed object   
        
        }
    }

thanks.

You have two "levels" of new , so three "levels" of delete can't be right.

Spell out your deletion loop, using indexing instead of pointer arithmetic:

First iteration:

delete ptr[0]+0;
delete ptr[0]+1;
...
delete ptr[0]+n-1;

delete [] ptr[0];

Second iteration:

delete ptr[1]+0;
delete ptr[1]+1;
...
delete ptr[1]+n-1;

delete [] ptr[1];

You're passing to delete a pointer to the first element of ptr[0] , a pointer to the second element of ptr[0] , a pointer to the third element of ptr[0] , ...
But the things you allocated were ptr[0] , ptr[1] , ... ptr[m-1] , not their individual elements.

Remove the innermost deletion loop.
(And don't mess around with pointer arithmetic when you can use indexing.)

I don't know how you would want to allocate memory space by m length if it is set to 0 by default. To me it looks like you set m = 0 and then try to allocate by 0 length or how do you control the length of your dimensions?

Maybe edit your constructor to:

MyMatrix(int m, int n)
{
    this->m = m;
    this->n = n;
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM