1

I have allocated a two dimensional array using the following code:

// Dynamic allocation
        int **matrix=new int*[n];
        for(int i=0;i<n;i++)
        {
            matrix[i]=new int[n];
        }

This works fine.

  • Firstly we allocate an array of integer pointers.
  • Then we further allocate each of the earlier pointer to point at a memory location of n integers. This creates our two dimensional array.

I know that a destructor for a dynamically allocated array should look like:

~SquareMatrix()
{
delete [] OneDarray;
}

The emphasis being on [] because if it is not written, only the first element of the array will be deleted.

On similar grounds, I guess I need to place the [] twice so as to delete the entire two dimensional array, like,

delete [] [] matrix;

But this is not working and giving a compile time error.

What is the correct way of doing it?

Akshay Arora
  • 1,953
  • 1
  • 14
  • 30
  • 2
    Destroy things exactly the *opposite* of how you allocated them. Just invoking `delete [] matrix` is not doing that. you used a loop for your construction, you need one for your destruction as well. (and ideally, don't do *any* of this in the first place: use `std::vector>` and let the standard lib work for you. Stop playing in the C++ sandbox with a C pail-and-shovel). – WhozCraig Feb 21 '15 at 18:58
  • possible duplicate of [C: Correctly freeing memory of a multi-dimensional array](http://stackoverflow.com/questions/1733881/c-correctly-freeing-memory-of-a-multi-dimensional-array) – Blob Feb 21 '15 at 18:59
  • @WhozCraig, Thank you. That is a point I will try not to forget ever. Do things exactly opposite of what I did in allocation. And yes, the school assignment requires it to be strictly an array and not a vector. :) – Akshay Arora Feb 21 '15 at 19:02
  • 1
    @AkshayArora too bad for that; best of luck. (and get ready to fulfill [The Rule of Three](http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) for that object class; you're going to need it). – WhozCraig Feb 21 '15 at 19:03
  • @WhozCraig, Wish I could "+1" your "answer". Comments give no reputation. Unfair. – Akshay Arora Feb 21 '15 at 19:05
  • 2
    Both answers below are more than adequate. No worries. – WhozCraig Feb 21 '15 at 19:06

2 Answers2

3

"What is the correct way of doing it?"

Using standard containers like e.g. std::vector<std::vector<int>> or std::array<std::array<int,N>,N> (supposed N is already known at compile time) is the correct way.
You don't use new()/new[] and delete/delete[] in 1st place (besides rare valid use cases).

If you're sure you have such case, you delete in exactly the reverse order as you were allocating using new[]. delete [][]; isn't valid syntax, as the compiler already told you.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
3
for(int i=0;i<n;i++)
{
    delete [] matrix[i];
}

delete [] matrix

You need to delete each of the internal arrays and then delete the array.

As has been suggested in a comment. A safer way of doing this would be to use standard containers such as std::vector. You could do something like this then:

std::vector<std::vector<int>> matrix;

This would give you a two dimensional array but you would not have to handle the cleanup.

pstrjds
  • 16,840
  • 6
  • 52
  • 61
  • 1
    [**give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime**](http://en.wiktionary.org/wiki/give_a_man_a_fish_and_you_feed_him_for_a_day;_teach_a_man_to_fish_and_you_feed_him_for_a_lifetime) – πάντα ῥεῖ Feb 21 '15 at 20:35