简体   繁体   中英

C++ delete on binary array of heap allocated data - does it delete it all? What should I know about delete?

I just had this question pop in my head as I was reviewing some of my code I've used for quite a while to handle binary character arrays of data.

Does the following semi-pseudo C++ code accurately free myBinaryDataArray from the heap if it is filled with binary data (including the '\\0' character)? I'm sure there is documentation somewhere, I did a few searches though and didn't find a result - it was probably my wording.

Code example:

char* myBinaryDataArray = new char[someLengthMyProgramNeeds];

/* mBinaryDataArray filled with binary data */

delete[] myBinaryDataArray;

I've been using the assumption that delete knows 'how' to delete correctly, but I recently had problems with delete on some objects after they were returned from a method (I noticed the destructor wasn't called). I'm now starting to question if I'm using it correctly.

Does anyone have a good resource or explanation on delete or how it works, and what I should keep in mind when using it?

Also just to make sure it wasn't me.. If someone could let me know if calling delete on a returned heap allocated pointer of an object is not correct, why not? I've found something that compiles by deleting a cast of it to a pointer of the type it's supposed to be, but I don't know if that's correct. I think I read something on it, but essentially I want to know more about delete in C++.

#include <iostream>

class foo {
public:
  ~foo() { std::cout << "Destructor called" << std::endl; }
};

class bar {
public:
  foo* myMethod() { return new foo(); }
};

int main(int argc, char** argv)
{
  bar b;
  foo* f = b.myMethod();

  delete f;
}

Output:

Destructor called

In response to:

Also just to make sure it wasn't me.. If someone could let me know if calling delete on a returned heap allocated pointer of an object is not correct, why not? I've found something that compiles by deleting a cast of it to a pointer of the type it's supposed to be, but I don't know if that's correct. I think I read something on it, but essentially I want to know more about delete in C++.

It doesn't make a difference whether you returned the pointer from a function call, as long as the pointer is intact. The only change in an object pointer should be done through the supported casts.

Even though new and delete are type-aware, they are still based on allocators and depend on the original pointer from new being passed back to delete . Think of new and delete as the same counter in a book library or video rental store.

The correct thing to do is to only delete objects that you originally allocated with new or new[] , or let a smart pointer take care of deleting for you.

The new should usually go into a constructor, and the delete into a corresponding destructor, but can be in a factory method or anywhere else.

The reason for C++ virtual destructor is to allow correct deletion of a polymorphic type. As long as Base and Derived implement correct virtual destructors, all class managed resources will be freed correctly.

The destructor will be executed based on the type information, but any code in the destructor will operate on the assumption that the original object lies at the pointer address. The allocator uses an algorithm like the buddy system that relies on where the pointer is, and bookkeeping information stashed at a relative offset of the object. If you pass delete an altered pointer, it is a bug and will usually corrupt the heap.

Does the following semi-pseudo C++ code accurately free myBinaryDataArray from the heap if it is filled with binary data (including the '\\0' character)?

No. If an exception or other control flow events (return comes to mind) occur before delete[] is called, it will leak.

I've been using the assumption that delete knows 'how' to delete correctly, but I recently had problems with delete on some objects after they were returned from a method (I noticed the destructor wasn't called). I'm now starting to question if I'm using it correctly.

If you're calling delete[] or delete yourself, the overwhelming likelihood is that you are not using it correctly. delete[] and delete are not useful for users.

Does anyone have a good resource or explanation on delete or how it works, and what I should keep in mind when using it?

There is nothing you need to keep in mind, since there is no need to use delete . It is a useful primitive for library implementers. Pretty much all the useful higher-level APIs on it are provided as Standard.

Even if you have some compelling use-case that absolutely cannot possibly be addressed by vector or unique_ptr / shared_ptr and friends in Boost, which is stupendously unlikely, there's still no motivation to use delete because any API worth using would take a deleter function and the Standard already provides one for delete .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM