views:

3326

answers:

7

What is the difference between "new" and "malloc" and "calloc" and others in family?

(When) Do I need anything other than "new" ?

Is one of them implemented using any other?

+8  A: 

new and delete are C++ specific features. They didn't exist in C. malloc is the old school C way to do things. Most of the time, you won't need to use it in C++.

  • malloc allocates uninitialized memory. The allocated memory has to be released with free.
  • calloc is like malloc but initializes the allocated memory with a constant (0). It needs to be freed with free.
  • new initializes the allocated memory by calling the constructor (if it's an object). Memory allocated with new should be released with delete (which in turn calls the destructor). It does not need you to manually specify the size you need and cast it to the appropriate type. Thus, it's more modern and less prone to errors.
Mehrdad Afshari
Is "scratch space" a term you have just invented?
anon
@Neil Butterworth: I don't know. But I think I've read it somewhere... Glossary: scratch space: n. uninitialized memory :)
Mehrdad Afshari
keeping c++ specific terminology, an array is an object too, and the ctors of all its sub-objects will be called.
Johannes Schaub - litb
I think it is confusing - we have enough terms in this area as it is. new allocates dynamic memory.
anon
Edited to stick with C++ terminology to reduce the chance of confusion.
Mehrdad Afshari
+4  A: 

Using new means that constructors will be called on the newly allocated memory. If the thing being allocated doesn't have constructors, new is functionally identical to malloc. and should normally be used in pereference to it.

new may or may not be implemented in terms of malloc - the C++ standard does not require either approach.

anon
+5  A: 
  • new allocates and calls to ctor (the order is unspecified), delete the dtor and frees the memory allocated by a call to new
  • malloc only allocates some memory, free deletes memory allocated by malloc
  • new may be implemented using malloc (not required though by the standard)
  • calloc does the same thing as malloc and also zeroizes the newly allocated memory

As other posts have pointed out: malloc/free is part of C++ to be compatible with C.

Also see: Stroustrup: new vs malloc

dirkgently
+1  A: 

the main difference between new and malloc I can recall is that you cannot reallocate memory allocated by new using realloc. So if you wanted to increase/decrease the size of the memory block, you had to allocate a new block and copy everything over.

Calloc allows you to initialize the memory block you allocate while malloc does not.

yx
In fact, this is the same for `malloc`. The `realloc` function does this most of the time.
Mehrdad Afshari
+1  A: 

When you new an object, space for the object is not only allocated but the object's constructor is called. But this is the C++ way its done, malloc is the old version way in C of allocating memory. calloc is the same as malloc, except for it clears memory to all bits zero.

TStamper
+4  A: 

new/delete + new[]/delete[]:

  • new/delete is the C++ way to allocate memory and deallocate memory from the heap.
  • new[] and delete[] is the c++ way to allocate arrays of contiguous memory.
  • Should be used because it is more type safe than malloc
  • Should be used because it calls the constructor/destructor
  • Cannot be used in a realloc way, but can use placement new to re-use the same buffer of data
  • Data cannot be allocated with new and freed with free, nor delete[]

malloc/free + family:

  • malloc/free/family is the C way to allocate and free memory from the heap.
  • calloc is the same as malloc but also initializes the memory
  • Should be used if you may need to reallocate the memory
  • Data cannot be allocated with malloc and freed with delete nor delete[]

Also see my related answer here

Brian R. Bondy
I recognize that format :-)
Martin York
@Martin York: I see now from the linked duplicate question. I like your lists better though :) I think it is the bets format for these types of questions where you compare and contrast 2 things. At least I always get high up votes for that type of formatted answers.
Brian R. Bondy
+1  A: 

You don't need anything other than new. It is a complete replacement for malloc in C++.

As for the difference: Malloc just allocates memory. New allocated memory and calls the constructors. Likewise free just releases the memory. Delete releases the memory and calls the destructor.

A word of warning: Don't mix the two idioms. The results are undefined.

Steve Rowe