0

Possible Duplicate:
How does delete[] “know” the size of the operand array?
How does the delete in C++ know how many memory locations to delete

I know it's a rather simple question but I a not sure about the difference (if any) between this lines :

double * a = new double[100];
delete[] a;
delete a;
free ((void*)a);

First off, would all of these calls (used each without the others) work the same way and free sizeof(double)*100 bytes? Which lead me to the 2nd question, how does the program keep track of the size of the allocated memory? For instance if I send my a pointer to a function, then delete[] this pointer from within my function, would I also free the same amount of memory?

Thanks

Community
  • 1
  • 1
lezebulon
  • 7,607
  • 11
  • 42
  • 73

3 Answers3

4

The difference, oversimplified, is this:

delete[] a;

Is correct. All others are incorrect, and will exhibit Undefined Behavior.

Now, in reality, on all the compilers I use daily, delete a; will do the right thing every time. But you should still not do it. Undefined Behavior is never correct.

The free call will also probably do the right thing in the real world, but only because the thing you're freeing doesn't have a non-default destructor. If you tried to free something that was a class with a destructor, for example, it definitely wouldn't work -- the destructor would never be called.

That's one of the big differences (not the only difference) between new/delete and malloc/free -- the former call the constructors and destructors, while the latter meerly allocate and dealocate space.

Incorporating something @Rob said in his now-deleted post:

The simple rule is this: every new[] requires exactly one delete[]. Every new requires exactly one delete. malloc requires free. No mix-and-match is allowed.

As to the question of how delete[] knows how many elements to delete, please see this response to a previous duplicate question.

Community
  • 1
  • 1
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • but this doesn't answer the question of how `delete[]` keeps track of the number of elements. – Nathan Fellman May 11 '12 at 19:59
  • @NathanFellman: True, but I thought that was adequately addressed in [the linked duplicate](http://stackoverflow.com/questions/197675/how-does-delete-know-the-size-of-the-operand-array). There were really two questions here -- I answered one, and the dupe answered the other. – John Dibling May 11 '12 at 20:00
  • thanks for the answer. what I am concerned about is why does the following code (from http://stackoverflow.com/questions/10552656/is-the-following-new-overload-leaking-memory) doesn't leak when I destroy the class: class a { public: void * operator new(size_t l, int nb); double values; }; void *a::operator new (size_t l,int n) { return new char[l+ (n>1 ? n - 1 : 0)*sizeof(double)]; } If I understand correctly when I call delete on a "a" element, only the 1st char would be delete since I am not calling the [] version – lezebulon May 11 '12 at 20:02
  • @lezebulon: The code in the linked post also exhibits Undefined Behavior by the fact that there is no `operator delete` provided by the class to go along with the `operator new` it provides. (more) – John Dibling May 11 '12 at 20:07
  • Now that being said, I would speculate (and this is only speculation) that the memory will not leak because of how memory managers typically work. Allocation blocks are often mapped by address. So when you `delete` the pointer, the correct allocation block is looked up in the map, even if you don't `delete` it correctly. – John Dibling May 11 '12 at 20:08
  • Ok thanks, so basically this is as much incorrect as calling "delete a" in my first question? – lezebulon May 11 '12 at 20:11
  • It is incorrect, yes. It might work, but it's still wrong. – John Dibling May 11 '12 at 20:11
  • @lezebulon: You're welcome. You might also find [this](http://blogs.msdn.com/b/oldnewthing/archive/2004/02/03/66660.aspx) interesting and pertinent. – John Dibling May 11 '12 at 20:15
0

Someone correct me, if I'm wrong, but as far as I understand, you use delete when you previously allocated memory with new and delete[] after using new Type[]. And free is used when you have allocated memory using malloc.

See c++ reference on delete and free.

bat.mango
  • 35
  • 7
0

Regarding the array of doubles, the result of all forms is the same -- all the allocated memory is returned to the system. The difference in calling free vs. delete vs. delete[] is:

  • free only releases the memory (the size of memory allocated for a was stored by memory manager when calling new)
  • delete calls the destructor of allocated object before releasing the memory
  • delete[] calls the destructor of each element in the array before releasing the memory

The difference is important if destructor of the allocated object contains cleanup code which releases other memory or system resource such as file or socket descriptor allocated during the lifetime of an object.

It is a good habit in C++ to allways use deleteon single instances and delete[] on array of objects/primitives regardless of the content of destructor.

Aleš Kotnik
  • 2,654
  • 20
  • 17