What is the difference between "new" and "malloc" and "calloc" in C ++?

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

(When) Do you need anything other than the "new"?

Is one of them implemented using any other?

+47
c ++ memory-management heap malloc
Apr 30 '09 at 16:47
source share
7 answers

new and delete are C ++ specific functions. They did not exist in C. malloc - it is an old school capable of doing things. In most cases, you do not need to use it in C ++.

  • malloc allocates uninitialized memory. The allocated memory must be freed using free .
  • calloc is similar to malloc , but initializes the allocated memory with constant (0). It needs to be freed using free .
  • new initializes the allocated memory by calling the constructor (if it is an object). Memory allocated with new must be released with delete (which, in turn, calls the destructor). You do not need to manually specify the required size and apply it to the appropriate type. Thus, it is more modern and less error prone.
+65
Apr 30 '09 at 16:49
source share

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

  • new / delete is a C ++ way to allocate memory and free memory from the heap.
  • new [] and delete [] is a C ++ method for allocating continuous memory arrays.
  • Should be used as it is safer in type than malloc
  • Must be used as it calls the constructor / destructor
  • Cannot be used in realloc mode, but can use a new location to reuse the same data buffer.
  • Data cannot be allocated new and released using free or remote []

malloc / free + family:

  • malloc / free / family is a way to transfer and free memory from the heap.
  • calloc is the same as malloc, but also initializes memory
  • Should be used if you need to reallocate memory.
  • Data cannot be allocated using malloc and freed with deletion or deletion []

Also see my answer here

+16
Apr 30 '09 at 16:51
source share
  • new allocates and calls ctor (no order is specified), deletes dtor and frees memory allocated by the call for the new
  • malloc only allocates some memory, frees memory allocated by malloc
  • new can be implemented using malloc (not necessarily standard)
  • calloc does the same as malloc and also resets the newly allocated memory

As other posts have pointed out: malloc / free is part of C ++ for C compatibility.

Also see: Stroustrup: new vs malloc

+8
Apr 30 '09 at 16:51
source share

Using new tools that will call constructors for newly allocated memory. If the selected thing has no constructors, the new one is functionally identical to malloc. and usually should be used in relation to it.

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

+5
Apr 30 '09 at 16:50
source share

You do not need anything but a new one. This is a complete replacement for malloc in C ++.

As for the difference: Malloc just allocates memory. The new allocated memory calls the constructors. Also frees up memory for free. Delete frees memory and calls the destructor.

A word of warning: do not mix two idioms. Results: undefined.

+5
Apr 30 '09 at 16:51
source share

The main difference between new and malloc I can remember is that you cannot reallocate the memory allocated by the new one using realloc. Therefore, if you wanted to increase / decrease the size of the memory block, you had to select a new block and copy everything.

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

+1
Apr 30 '09 at 16:51
source share

When you are a new object, space for the object is not only allocated, but the constructor of the object is called. But this is the C ++ method that he did, malloc is the old version in C of memory allocation. calloc is the same as malloc , except that it clears the memory to all bits 0.

+1
Apr 30 '09 at 16:51
source share



All Articles