Gilad
Gilad

Reputation: 6575

memory allocation and deallocation of struct c++

struct SAD_tables
{
    typedef int** sadTable;
    sadTable sadTables[16];
    int height;
    int width;

    SAD_tables(int _height, int _width)
    {
        height = _height;
        width = _width;
        for (int i = 0; i < 16; i++)
        {
            sadTables[i] = new int*[height];
            for (int j = 0; j < height; j++)
            {
                sadTables[i][j] = new int[width];
            }
        }
    }

    ~SAD_tables()
    {       
        for (int i = 0; i < 16; i++)
        {
            for (int j = 0; j < height; j++)
            {
                delete sadTables[i][j];
            }
            delete[] sadTables[i];
        }
    }
};

i'm not sure if i have implemented my delete function correctly. can you please explain if i'm using delete[] and delete correctly?

Upvotes: 0

Views: 254

Answers (1)

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

If you insist on handling memory manually instead of using a proper container class, you need to fix new/delete. Each new call you have is in the array form new ...[] so each delete should be delete[], but you use delete without [] in the loop.

Upvotes: 2

Related Questions