Delete pointer to multidimensional array in class

2020-01-29 20:27发布

问题:

I have a pointer to a class, that have a pointer to a multidimensional array but I can't seem to delete it from memory when I need to or set it to NULL.

#define X 10
#define Y 10

struct TestClass
{
public:
       int      *pArray[X][Y];
};


// different tries, none working:

delete Pointer_To_TestClass->pArray[0][0];
delete[] Pointer_To_TestClass->pArray[0][0]

// or by simply:

Pointer_To_TestClass->pArray[0][0] = NULL;

I know the array has data because I can see the results on screen. Also check if it's NULL already, then doesn't try to delete it.

Since I want to delete a pointer in another pointer - is this a special circumstance that works differently? Like it deletes the first pointer holding the other pointer instead of the pointer inside the pointer (pArray is the second pointer, Pointer_To_Testclass is the first pointer)

UPDATE/EXPLANATION

I want to be able to delete pArray[0][0] while pArray[0][1] still exists and if [0][0] doesn't exist it should be equal to NULL. Most because I want to access this array by [X][Y] values for easy access. If [0][0] is a pointer, it should be NULL when deleted so I can check if it is NULL.

Anyone has any ideas?

回答1:

If you want a 2D array of pointers to <whatever>, create a class to handle that, then put an instance of it in your TestClass. As far as how to do that, I'd generally use something on this order:

template <class T>
class matrix2d {
    std::vector<T> data;
    size_t cols;
    size_t rows;
public:
    matrix2d(size_t y, size_t x) : cols(x), rows(y), data(x*y) {}
    T &operator()(size_t y, size_t x) { 
        assert(x<=cols);
        assert(y<=rows);
        return data[y*cols+x];
    }
    T operator()(size_t y, size_t x) const { 
        assert(x<=cols);
        assert(y<=rows);
        return data[y*cols+x];
    }
};

class TestClass { 
    matrix2d<int *> array(10, 10);
public:
    // ...
};

Given that you're storing pointers, however, you might want to consider using Boost ptr_vector instead of std::vector.



回答2:

#define X 10
#define Y 10

struct TestClass
{
public:
  TestClass()
  {
    // Must initialize pArray to point to real int's otherwise pArray
    // will have bogus pointers.
    for (size_t y = 0; y < Y; ++y)
      {
    for (size_t x = 0; x < X; ++x)
      {
        pArray[x][y] = new int;
      }
      }
  }

  int      *pArray[X][Y];
};

int main()
{
  TestClass *Pointer_To_TestClass = new TestClass;
  delete Pointer_To_TestClass->pArray[0][0];
  Pointer_To_TestClass->pArray[0][0] = 0;
  return 0;
}


回答3:

Depends on your allocation of pArray[i][j]. If it's something like pArray[i][j] = new int; then call delete Pointer_To_TestClass->pArray[0][0];. If it's something like pArray[i][j] = new int[10] then use your second option, delete[] Pointer_To_TestClass->pArray[0][0];. I'd also highly recommend immediately following the delete with setting it to NULL and proceeding it with the check for NULL.

The third one did not compile for me delete[][] Pointer_To_TestClass->pArray[0][0]; I got the error

Line 34: error: expected primary-expression before '[' token compilation terminated due to -Wfatal-errors.

Also, I'd highly recommend having those deletes in the destructor. Other places may be fine as well.

You may find the typedef of int* to be helpful for readability understanding you have a two-dimensional array of int*.



回答4:

If you've dynamically created an array, then delete it with delete[]. You have to give delete[] a pointer to the start of the array, however, not an element of the array!

If you want the elements to be deletable, then they have to be dynamically allocated separately.

So you have to have a multi-dimensional array of pointers;

int *(*pArray)[X][Y];

Then dynamically assign each one, which yes is a pain.

It is interesting, however, that you are even attempting to do this, why do you want to delete single elements?