1

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?

21
  • 5
    That has nothing to do with it, though. It's a multidimensional array of pointers, not a pointer to a multidimensional array. Commented Aug 17, 2012 at 16:48
  • 2
    Something tells me you need to rethink your code. There's bound to be a better way. Commented Aug 17, 2012 at 16:49
  • 3
    Please consider using std::vector for arrays in C++. A pointer to a jagged 2D array would then be declared as std::vector<std::vector<int> >*. Commented Aug 17, 2012 at 16:51
  • 6
    I would recommend following the rule of zero instead. That said, the answer to "how do I delete this thing?" depends on the answer to "how did I allocate this thing?", which was not included in the question. Commented Aug 17, 2012 at 16:53
  • 1
    My question is still, why can I create a pointer: Object *Object[X][Y] and store data in it (with pointers, through Object[X][Y] = new Object() but not delete the data since it IS a pointer.) Commented Aug 17, 2012 at 17:01

4 Answers 4

6

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.

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

2 Comments

You are kinda re-inventing boost::multi_array here.
The coordinates are backwards. Incrementing the second coordinate should be the small jump, incrementing the first coordinate should be the larger jump. As it is, a for(i) for(j) nested loop needs to say mat(j, i) to get the right memory traversal.
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;
}

5 Comments

But I don't want to reset the whole array (pArray) I want to reset the values inside the array [x][y] so I can check if the value is NULL or not. If NULL will skip, else will do something with the data. If I have a normal array the value isn't NULL. I might need pArray[0][0] but not pArray[0][1] for example, therefor pArray[0][1] should be deletable while pArray[0][0] isn't deleted.
...and the object pArray is not of "ints" it's of objects (class created) so I need to be able to set it as NULL.
...which I can't with this method.
@Deukalion I'm sorry my assumption that you wanted a 2 dimensional array of ints was incorrect. Are you initializing pArray? At first when it is created it will have garbage, it will have whatever data is left over on the stack or heap. If it isn't initialized, you will likely get errors when you try to delete it.
I changed the code in my comment above to show one way that pArray could be initialized.
0

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*.

Comments

0

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?

14 Comments

I have a grid of a set size, depending on my program it should render stuff on each of these X and Y coordinates but if not it should not even be stored in memory because it isn't needed. Or I might want to delete a big chuck of it in one go and also be able to check what fields in Grid is occupied and which is not.
Simply put, once I'm done with the object[X][Y] it's not needed anymore.
Then maybe when the 'object' class should have a de-activated mode? One that deletes most of the information ready to be activated again.
You didn't write how to delete the objects once I've set them. I want the elemens not needed not to be "present" and when I'm done with it I want to delete just an object in that array. I guess the pointer to the object that holds the pointer to this array is deleted when I try this.
If you did it as I said earlier, then use delete pArray[i][j]; as you did before to delete.
|

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.