3

I am wondering how can i use the delete operator if I want to delete an array initialized like this:

int (*my_ptr)[10] = new int[3][10];

This seems invalid:

delete[][]
1
  • 1
    delete[][] is invalid. Use delete[]. It will should call all destructors , as required. Commented Mar 30, 2020 at 14:17

2 Answers 2

5

new int[3][10] is simply creation of a dynamic array whose elements are themselves arrays. This is deleted in the same way as all dynamic arrays: delete[].

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

1 Comment

@asmmo That might be confusing since sub objects do not have static storage.
4

my_ptr is an array of 3 elements. Each element is an int[10] type.

You call delete[] my_ptr; to delete those 3 elements.

1 Comment

It's actually an array of 3 arrays of 10 elements. I got this wrong too first. It's strange, but the dynamic part is in the middle of the element type.

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.