2

Possible Duplicate:
Is there any danger in calling free() or delete instead of delete[]?

I was reading this question:
In what cases do I use malloc vs new?

Someone raised that one reason to use malloc was if you were going to use free.

I was wondering: Is it valid to mix a free call and a constructor initialization in C++?

i.e.

Can I say:

my_type *ptr = new my_type;
free(my_type);

Is that somehow invalid or worse than:

my_type *ptr = new my_type;
delete my_type;

other than the fact that it's not c++ish?

Likewise, could you do the opposite? Can you say

my_type *ptr = (my_type *)malloc(sizeof(my_type));
delete my_type;

Please merge if this is a duplicate, I searched but didn't see a question along this lines exactly about malloc/delete/new/free asked.

Community
  • 1
  • 1
Jason R. Mick
  • 5,177
  • 4
  • 40
  • 69
  • Question is a dupe ( of a dupe ). http://stackoverflow.com/questions/1612031/is-there-any-danger-in-calling-free-or-delete-instead-of-delete – Serapth Sep 10 '10 at 17:52
  • 2
    If you want to use malloc/free with C++ objects, you can use malloc and placement new operator to call a constructor and than call delete on it, and call free to release the memory. Or create a custom allocator when applicable. –  Sep 10 '10 at 17:55
  • 2
    @Vlad: Even after using placement new, you still can't call delete. But you can manually call the object's destructor before sending the memory to free. – Dennis Zickefoose Sep 10 '10 at 18:13
  • @Dennis, yeah you are probably right. Manual call to destructor and fee() is the way to go. Its been a long time since I used it :-> –  Sep 10 '10 at 18:27
  • Just a minor note, my question is an exact dupe of the listed question. The listed question doesn't cover malloc. However, it does cover delete, delete[], and new, so it is mostly the same... – Jason R. Mick Sep 10 '10 at 18:41

4 Answers4

11

No it is invalid. There is no guarantee that new will use malloc or delete will use free.

Moreover, using free instead of delete will skip my_type's destructor. If my_type itself is holding some resources, those will be leaked. Similarly, malloc will skip the constructor so the variable may be in an invalid state.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
5

Is it valid to mix a free call and a constructor initialization in C++?

No it is not.

malloc,calloc,realloc -> free

new -> delete

new[] -> delete[]

nos
  • 223,662
  • 58
  • 417
  • 506
2

No, this is not valid. malloc (and other C allocation functions) must be matched with free, new must be matched with delete, and new [] must be matched with delete []. While your compiler may not necessarily do anything differently if there is no destructor code, this is not something you should rely on.

The main difference is that new/delete call the constructor and destructor of an object; malloc and free just treat it as raw, untyped memory.

Michael Madsen
  • 54,231
  • 8
  • 72
  • 83
2
  1. free() does not call the destructor. It just deallocates the memory, no questions asked.

  2. new/delete is not guaranteed to use malloc() as its memory allocator; free might not even know about the memory you're throwing at it

tdammers
  • 20,353
  • 1
  • 39
  • 56