1

How do I delete this allocated pointer?

int (*foo)[4] = new int[100][4];

Is it just :

delete[] foo;
5
  • 2
    You're using new T[] with T being int[100]. Therefore, you need delete[], like any other T. Commented Jul 30, 2014 at 17:22
  • 1
    @chris, isn't the type int[4] actually? int (*foo)[4] declares a pointer to array-of-4-int, then you allocate 100 such arrays. Commented Jul 30, 2014 at 18:17
  • @vsoftco, Oops, that's right. Not sure why I put int[100]. Commented Jul 30, 2014 at 18:26
  • First I am not sure if that`s how you initialize a double pointer. Second, its seems you have a pointer foo; that points to four pointers. To avoid memory leak you have to delete all pointers and not just one. So you need to: delete foo[0]; delete foo[1]; delete foo[2]; delete foo[3]; and then delete foo; All these are pointers. Commented Jul 30, 2014 at 18:45
  • @Juniar, it is not a double pointer, it is a pointer to a data type defined as int[4] (that occupies 4 contiguous ints), so in the memory looks like {X X X X} -> {X X X X} -> ... 100 times ... -> {X X X X } Commented Jul 30, 2014 at 18:47

1 Answer 1

5

As you have allocated an array you have to use operator delete[]

delete []foo;

That it would be more clear you can rewrite the code snippet the following way

typedef int T[4];

T *foo = new T[100];

delete []foo;
Sign up to request clarification or add additional context in comments.

Comments

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.