The new and malloc() operators are both used to allocate memory dynamically in C++. However, there are some key differences between the two operators.
new is an operator, while malloc() is a function. This means that
new can be used with expressions, while malloc() cannot. For example, we can write
int* p = new int[10], but we cannot write int* p = malloc(sizeof(int) * 10).
new calls the constructor of the type being allocated, while
malloc() does not. This means that if we allocate an object of a class type using
new, the class's constructor will be called automatically. If we allocate an object of a primitive type using
new, the default constructor will be called automatically.
new throws an exception if it fails to allocate memory, while
malloc() returns a null pointer. This means that if we use new to allocate memory and the allocation fails, our program will crash. If we use
malloc() to allocate memory and the allocation fails, our program will continue to run, but the pointer we get back will be null.
In general, it is recommended to use new instead of malloc() for memory allocation in C++.
new is more convenient to use, and it is safer because it will not crash your program if it fails to allocate memory. However, there are some cases where you may need to use
malloc() instead of new. For example, if you are working with a legacy C code that uses
malloc(), you may need to use malloc() in your C++ code to maintain compatibility.
Here is a table summarizing the differences between new and
malloc():
Feature
new
malloc()
Type
Operator
Function
Calls constructor
Yes
No
Throws exception on failure
Yes
No
Returns null pointer on failure
No
Yes
Recommended for use
Yes
No (unless necessary)
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The
newandmalloc()operators are both used to allocate memory dynamically in C++. However, there are some key differences between the two operators.newis an operator, whilemalloc()is a function. This means thatnewcan be used with expressions, whilemalloc()cannot. For example, we can writeint* p = new int[10], but we cannot writeint* p = malloc(sizeof(int) * 10).newcalls the constructor of the type being allocated, whilemalloc()does not. This means that if we allocate an object of a class type usingnew, the class's constructor will be called automatically. If we allocate an object of a primitive type usingnew, the default constructor will be called automatically.newthrows an exception if it fails to allocate memory, whilemalloc()returns a null pointer. This means that if we usenewto allocate memory and the allocation fails, our program will crash. If we usemalloc()to allocate memory and the allocation fails, our program will continue to run, but the pointer we get back will be null.In general, it is recommended to use
newinstead ofmalloc()for memory allocation in C++.newis more convenient to use, and it is safer because it will not crash your program if it fails to allocate memory. However, there are some cases where you may need to usemalloc()instead ofnew. For example, if you are working with a legacy C code that usesmalloc(), you may need to usemalloc()in your C++ code to maintain compatibility.Here is a table summarizing the differences between
newandmalloc():newmalloc()