[c++] What is the difference between new/delete and malloc/free?

What is the difference between new/delete and malloc/free?

Related (duplicate?): In what cases do I use malloc vs new?

This question is related to c++ memory-management

The answer is


new/delete is C++, malloc/free comes from good old C.

In C++, new calls an objects constructor and delete calls the destructor.

malloc and free, coming from the dark ages before OO, only allocate and free the memory, without executing any code of the object.


The most relevant difference is that the new operator allocates memory then calls the constructor, and delete calls the destructor then deallocates the memory.


new and delete are C++ primitives which declare a new instance of a class or delete it (thus invoking the destructor of the class for the instance).

malloc and free are C functions and they allocate and free memory blocks (in size).

Both use the heap to make the allocation. malloc and free are nonetheless more "low level" as they just reserve a chunk of memory space which will probably be associated with a pointer. No structures are created around that memory (unless you consider a C array to be a structure).


This code for use of delete keyword or free function. But when create a pointer object using 'malloc' or 'new' and deallocate object memory using delete even that object pointer can be call function in the class. After that use free instead of delete then also it works after free statement , but when use both then only pointer object can't call to function in class.. the code is as follows :

#include<iostream>


using namespace std;

class ABC{
public: ABC(){
    cout<<"Hello"<<endl;
  }

  void disp(){
    cout<<"Hi\n";
  }

};

int main(){

ABC* b=(ABC*)malloc(sizeof(ABC));
int* q = new int[20];
ABC *a=new ABC();
b->disp();

cout<<b<<endl;
free(b);
delete b;
//a=NULL;
b->disp();
ABC();
cout<<b;
return 0;
}

output :

Hello
Hi
0x2abfef37cc20

also,

the global new and delete can be overridden, malloc/free cannot.

further more new and delete can be overridden per type.


The most relevant difference is that the new operator allocates memory then calls the constructor, and delete calls the destructor then deallocates the memory.


new and delete are operators in c++; which can be overloaded too. malloc and free are function in c;

malloc returns null ptr when fails while new throws exception.

address returned by malloc need to by type casted again as it returns the (void*)malloc(size) New return the typed pointer.


The only similarities are that malloc/new both return a pointer which addresses some memory on the heap, and they both guarantee that once such a block of memory has been returned, it won't be returned again unless you free/delete it first. That is, they both "allocate" memory.

However, new/delete perform arbitrary other work in addition, via constructors, destructors and operator overloading. malloc/free only ever allocate and free memory.

In fact, new is sufficiently customisable that it doesn't necessarily return memory from the heap, or even allocate memory at all. However the default new does.


new/delete is C++, malloc/free comes from good old C.

In C++, new calls an objects constructor and delete calls the destructor.

malloc and free, coming from the dark ages before OO, only allocate and free the memory, without executing any code of the object.


  • new is an operator, whereas malloc() is a fucntion.
  • new returns exact data type, while malloc() returns void * (pointer of type void).
  • malloc(), memory is not initialized and default value is garbage, whereas in case of new, memory is initialized with default value, like with 'zero (0)' in case on int.
  • delete and free() both can be used for 'NULL' pointers.

1.new syntex is simpler than malloc()

2.new/delete is a operator where malloc()/free() is a function.

3.new/delete execute faster than malloc()/free() because new assemly code directly pasted by the compiler.

4.we can change new/delete meaning in program with the help of operator overlading.


The main difference between new and malloc is that new invokes the object's constructor and the corresponding call to delete invokes the object's destructor.

There are other differences:

  • new is type-safe, malloc returns objects of type void*

  • new throws an exception on error, malloc returns NULL and sets errno

  • new is an operator and can be overloaded, malloc is a function and cannot be overloaded

  • new[], which allocates arrays, is more intuitive and type-safe than malloc

  • malloc-derived allocations can be resized via realloc, new-derived allocations cannot be resized

  • malloc can allocate an N-byte chunk of memory, new must be asked to allocate an array of, say, char types

Looking at the differences, a summary is malloc is C-esque, new is C++-esque. Use the one that feels right for your code base.

Although it is legal for new and malloc to be implemented using different memory allocation algorithms, on most systems new is internally implemented using malloc, yielding no system-level difference.


There are a few things which new does that malloc doesn’t:

  1. new constructs the object by calling the constructor of that object
  2. new doesn’t require typecasting of allocated memory.
  3. It doesn’t require an amount of memory to be allocated, rather it requires a number of objects to be constructed.

So, if you use malloc, then you need to do above things explicitly, which is not always practical. Additionally, new can be overloaded but malloc can’t be.

In a word, if you use C++, try to use new as much as possible.


new calls the ctor of the object, delete call the dtor.

malloc & free just allocate and release raw memory.


new/delete is C++, malloc/free comes from good old C.

In C++, new calls an objects constructor and delete calls the destructor.

malloc and free, coming from the dark ages before OO, only allocate and free the memory, without executing any code of the object.


new and delete are C++ primitives which declare a new instance of a class or delete it (thus invoking the destructor of the class for the instance).

malloc and free are C functions and they allocate and free memory blocks (in size).

Both use the heap to make the allocation. malloc and free are nonetheless more "low level" as they just reserve a chunk of memory space which will probably be associated with a pointer. No structures are created around that memory (unless you consider a C array to be a structure).


new calls the ctor of the object, delete call the dtor.

malloc & free just allocate and release raw memory.


  • To use the malloc(), we need to include <stdlib.h> or <alloc.h> in the program which is not required for new.
  • new and delete can be overloaded but malloc can not.
  • Using the placement new, we can pass the address where we want to allocate memory but this is not possible in case of malloc.

new calls the ctor of the object, delete call the dtor.

malloc & free just allocate and release raw memory.


This code for use of delete keyword or free function. But when create a pointer object using 'malloc' or 'new' and deallocate object memory using delete even that object pointer can be call function in the class. After that use free instead of delete then also it works after free statement , but when use both then only pointer object can't call to function in class.. the code is as follows :

#include<iostream>


using namespace std;

class ABC{
public: ABC(){
    cout<<"Hello"<<endl;
  }

  void disp(){
    cout<<"Hi\n";
  }

};

int main(){

ABC* b=(ABC*)malloc(sizeof(ABC));
int* q = new int[20];
ABC *a=new ABC();
b->disp();

cout<<b<<endl;
free(b);
delete b;
//a=NULL;
b->disp();
ABC();
cout<<b;
return 0;
}

output :

Hello
Hi
0x2abfef37cc20

new and delete are C++ primitives which declare a new instance of a class or delete it (thus invoking the destructor of the class for the instance).

malloc and free are C functions and they allocate and free memory blocks (in size).

Both use the heap to make the allocation. malloc and free are nonetheless more "low level" as they just reserve a chunk of memory space which will probably be associated with a pointer. No structures are created around that memory (unless you consider a C array to be a structure).


new and delete are operators in c++; which can be overloaded too. malloc and free are function in c;

malloc returns null ptr when fails while new throws exception.

address returned by malloc need to by type casted again as it returns the (void*)malloc(size) New return the typed pointer.


also,

the global new and delete can be overridden, malloc/free cannot.

further more new and delete can be overridden per type.


In C++ new/delete call the Constructor/Destructor accordingly.

malloc/free simply allocate memory from the heap. new/delete allocate memory as well.


The most relevant difference is that the new operator allocates memory then calls the constructor, and delete calls the destructor then deallocates the memory.


also,

the global new and delete can be overridden, malloc/free cannot.

further more new and delete can be overridden per type.


  • To use the malloc(), we need to include <stdlib.h> or <alloc.h> in the program which is not required for new.
  • new and delete can be overloaded but malloc can not.
  • Using the placement new, we can pass the address where we want to allocate memory but this is not possible in case of malloc.

new and delete are C++ primitives which declare a new instance of a class or delete it (thus invoking the destructor of the class for the instance).

malloc and free are C functions and they allocate and free memory blocks (in size).

Both use the heap to make the allocation. malloc and free are nonetheless more "low level" as they just reserve a chunk of memory space which will probably be associated with a pointer. No structures are created around that memory (unless you consider a C array to be a structure).


There are a few things which new does that malloc doesn’t:

  1. new constructs the object by calling the constructor of that object
  2. new doesn’t require typecasting of allocated memory.
  3. It doesn’t require an amount of memory to be allocated, rather it requires a number of objects to be constructed.

So, if you use malloc, then you need to do above things explicitly, which is not always practical. Additionally, new can be overloaded but malloc can’t be.

In a word, if you use C++, try to use new as much as possible.


also,

the global new and delete can be overridden, malloc/free cannot.

further more new and delete can be overridden per type.


  • new is an operator, whereas malloc() is a fucntion.
  • new returns exact data type, while malloc() returns void * (pointer of type void).
  • malloc(), memory is not initialized and default value is garbage, whereas in case of new, memory is initialized with default value, like with 'zero (0)' in case on int.
  • delete and free() both can be used for 'NULL' pointers.

In C++ new/delete call the Constructor/Destructor accordingly.

malloc/free simply allocate memory from the heap. new/delete allocate memory as well.


The only similarities are that malloc/new both return a pointer which addresses some memory on the heap, and they both guarantee that once such a block of memory has been returned, it won't be returned again unless you free/delete it first. That is, they both "allocate" memory.

However, new/delete perform arbitrary other work in addition, via constructors, destructors and operator overloading. malloc/free only ever allocate and free memory.

In fact, new is sufficiently customisable that it doesn't necessarily return memory from the heap, or even allocate memory at all. However the default new does.


In C++ new/delete call the Constructor/Destructor accordingly.

malloc/free simply allocate memory from the heap. new/delete allocate memory as well.


The only similarities are that malloc/new both return a pointer which addresses some memory on the heap, and they both guarantee that once such a block of memory has been returned, it won't be returned again unless you free/delete it first. That is, they both "allocate" memory.

However, new/delete perform arbitrary other work in addition, via constructors, destructors and operator overloading. malloc/free only ever allocate and free memory.

In fact, new is sufficiently customisable that it doesn't necessarily return memory from the heap, or even allocate memory at all. However the default new does.


new calls the ctor of the object, delete call the dtor.

malloc & free just allocate and release raw memory.


new/delete is C++, malloc/free comes from good old C.

In C++, new calls an objects constructor and delete calls the destructor.

malloc and free, coming from the dark ages before OO, only allocate and free the memory, without executing any code of the object.


1.new syntex is simpler than malloc()

2.new/delete is a operator where malloc()/free() is a function.

3.new/delete execute faster than malloc()/free() because new assemly code directly pasted by the compiler.

4.we can change new/delete meaning in program with the help of operator overlading.


The main difference between new and malloc is that new invokes the object's constructor and the corresponding call to delete invokes the object's destructor.

There are other differences:

  • new is type-safe, malloc returns objects of type void*

  • new throws an exception on error, malloc returns NULL and sets errno

  • new is an operator and can be overloaded, malloc is a function and cannot be overloaded

  • new[], which allocates arrays, is more intuitive and type-safe than malloc

  • malloc-derived allocations can be resized via realloc, new-derived allocations cannot be resized

  • malloc can allocate an N-byte chunk of memory, new must be asked to allocate an array of, say, char types

Looking at the differences, a summary is malloc is C-esque, new is C++-esque. Use the one that feels right for your code base.

Although it is legal for new and malloc to be implemented using different memory allocation algorithms, on most systems new is internally implemented using malloc, yielding no system-level difference.


The only similarities are that malloc/new both return a pointer which addresses some memory on the heap, and they both guarantee that once such a block of memory has been returned, it won't be returned again unless you free/delete it first. That is, they both "allocate" memory.

However, new/delete perform arbitrary other work in addition, via constructors, destructors and operator overloading. malloc/free only ever allocate and free memory.

In fact, new is sufficiently customisable that it doesn't necessarily return memory from the heap, or even allocate memory at all. However the default new does.