2

Is there some difference in the following deletions of object array?

The first way:

MyClass **obj = new MyClass*[NUM];
for (int i=0; i<NUM; i++) obj[i] = new MyClass(val);
obj[0]->method();
for (int i=0; i<NUM; i++) delete obj[i]; /// Deletion1
delete obj;                              /// Deletion1

The second way:

MyClass **obj = new MyClass*[NUM];
for (int i=0; i<NUM; i++) obj[i] = new MyClass(val);
obj[0]->method();
delete[] obj;                            /// Deletion2
obj = 0x0;                               /// Deletion2

Both ways are workable and look similar in debugger.

6
  • possible duplicate of Why is there a delete[] in C++? Commented Oct 10, 2010 at 3:48
  • 4
    "obj = 0x0; /// Deletion2" A great example of a comments being lies! Commented Oct 10, 2010 at 3:52
  • The correct answer is not at all. If you have to delete something manually, you've done it wrong. Commented Oct 10, 2010 at 4:39
  • Cris, I don't think so. Explanations, like you indicated, weren't enough for me. Commented Oct 10, 2010 at 4:47
  • Johnsyweb, it was rather a border flag, that a real comment. Clearly, the samples are investigating things like homework. Commented Oct 10, 2010 at 4:49

2 Answers 2

4

Both are incorrect. The correct way would be:

for (int i = 0; i < NUM; i++) delete obj[i];
delete[] obj;

In the first way you show, you use delete to destroy an object allocated with new[], which results in undefined behavior (if you use new[], you must destroy the object using delete[]).

In the second way you show, you leak all of the pointers that you created in the first for loop.

If you use std::vector instead of dynamically allocated arrays and some type of smart pointer, then you don't have to worry about this in most code.

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

Comments

3

In your first example, you are explicitly calling the destructor for each object pointed to by members of the allocated array. Then you are deleting the array of pointers (which should really be delete[] because you allocated it as an array, but in practice for this example it probably doesn't matter).

In your second example, you are only deleting the array of pointers, which does not call the destructor for the pointed-to objects. The reason it doesn't is that you may have made copies of those pointers in other variables which the compiler doesn't necessarily know about.

If you were to create an array of objects and not pointers, like this:

MyClass *obj = new MyClass[NUM];

then the delete[] operator would automatically call the destructor for each of the NUM objects in the allocated array.

1 Comment

Thanks Greg. The reason, why I didn't want to use the way you suggested, was using non-def constructor, new MyClass(val). I was just confused with the deletion doubt.

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.