new vs. malloc() — What's the Difference?
By Tayyaba Rehman — Published on January 3, 2024
new is a C++ operator that allocates memory and calls constructors for object initialization. malloc() is a C library function that allocates memory without initializing it.
Difference Between new and malloc()
Table of Contents
ADVERTISEMENT
Key Differences
The 'new' operator in C++ serves two primary functions: it allocates memory for an object and then initializes that object by calling its constructor. This initialization is a key aspect that differentiates 'new' from 'malloc()'. On the other hand, 'malloc()' is a standard library function in C that allocates a specified amount of memory but does not perform any initialization. It simply returns a pointer to the allocated memory without constructing any objects.
Memory allocation using 'new' is type-safe, meaning it allocates memory for a specific type and returns a pointer of that type. This helps in maintaining the integrity of the program by preventing type mismatches. In contrast, 'malloc()' is not type-safe. It allocates a specified number of bytes and returns a void pointer, which needs to be explicitly cast to the desired type, increasing the risk of errors.
Another significant difference is in error handling. When 'new' fails to allocate memory, it throws an exception, which can be caught and handled. This exception handling mechanism is not available with 'malloc()', which instead returns a NULL pointer if it fails, requiring different error-checking techniques.
In terms of memory deallocation, the counterpart of 'new' is 'delete', which not only frees the memory but also calls the destructor of the object, ensuring proper cleanup. The equivalent for 'malloc()' is 'free()', which merely frees the allocated memory without any concern for object destruction or cleanup.
Lastly, the syntax of 'new' is more aligned with C++’s object-oriented features, making it a more natural choice for C++ programming. 'malloc()', being a part of the C standard library, is more commonly used in C programs or where low-level memory management is required, without the need for object-oriented features.
ADVERTISEMENT
Comparison Chart
Function
Allocates memory and initializes objects.
Allocates memory without initialization.
Type Safety
Type-safe, returns a specific type pointer.
Not type-safe, returns a void pointer.
Error Handling
Throws an exception on failure.
Returns NULL on failure.
Counterpart for Deallocation
Paired with 'delete' for object destruction.
Paired with 'free()', no object destruction.
Usage
Commonly used in C++ for object-oriented programming.
Used in C for general-purpose memory allocation.
Compare with Definitions
New
'new' is paired with 'delete' for memory deallocation.
Delete ptr; deallocates memory and calls the destructor.
Malloc()
On allocation failure, 'malloc()' returns NULL.
Checking if(ptr == NULL) is essential after 'malloc()'.
New
'new' supports object-oriented programming in C++.
Auto obj = new MyClass(); uses 'new' for creating a class instance.
Malloc()
'malloc()' allocates un-initialized memory.
Int* ptr = (int*)malloc(sizeof(int)); allocates memory for an integer.
New
'new' is type-safe and returns a specific type pointer.
MyClass* obj = new MyClass(); creates an object of MyClass.
Malloc()
'malloc()' is commonly used in C programming.
Void* memory = malloc(50); allocates 50 bytes of memory.
New
'new' allocates and initializes objects in C++.
Int* ptr = new int(5); allocates memory for an integer and initializes it to 5.
Malloc()
'malloc()' pairs with 'free()' for deallocation.
Free(ptr); frees the allocated memory without calling destructors.
New
On failure, 'new' throws a bad_alloc exception.
Try-catch blocks handle exceptions when 'new' fails to allocate memory.
Malloc()
'malloc()' returns a void pointer, requiring type casting.
Char* buffer = (char*)malloc(100); allocates 100 bytes for a character array.
Malloc()
(computing) A subroutine in the C programming language's standard library for performing dynamic memory allocation.
Malloc()
(computing) To allocate memory using the C programming language malloc subroutine.
Common Curiosities
Is 'new' type-safe?
Yes, 'new' is type-safe and returns a pointer of the specific type.
What is 'malloc()' used for?
'malloc()' allocates a specified amount of un-initialized memory.
How is 'new' different from 'malloc()' in error handling?
'new' throws an exception, while 'malloc()' returns NULL on failure.
Can you use 'malloc()' in C++?
Yes, but 'new' is preferred for its object initialization and type safety.
Why is 'malloc()' not type-safe?
'malloc()' returns a void pointer, requiring explicit casting to the desired type.
What does 'new' do in C++?
'new' allocates memory and initializes objects in C++.
What is the counterpart of 'new' for memory deallocation?
The counterpart is 'delete', which also calls the destructor.
In which scenarios is 'new' preferred over 'malloc()'?
In object-oriented programming and when object initialization is required.
How do you deallocate memory allocated by 'malloc()'?
Use 'free()' to deallocate memory allocated by 'malloc()'.
Can 'malloc()' initialize objects?
No, 'malloc()' only allocates memory without initialization.
Is 'new' exclusive to C++?
Yes, it is specifically designed for C++ programming.
What happens if you use 'free()' on memory allocated by 'new'?
It can lead to undefined behavior, as 'free()' does not call destructors.
How do you handle exceptions thrown by 'new'?
Use try-catch blocks to handle exceptions from 'new'.
Does 'malloc()' have an equivalent in C++?
'malloc()' can be used in C++, but 'new' is the C++-specific equivalent.
Why is type casting necessary with 'malloc()' in C++?
Because 'malloc()' returns a void pointer, it must be cast to the appropriate type.
Share Your Discovery
Previous Comparison
Darwinism vs. Neo-DarwinismNext Comparison
Addition Polymerization vs. Condensation PolymerizationAuthor Spotlight
Written by
Tayyaba RehmanTayyaba Rehman is a distinguished writer, currently serving as a primary contributor to askdifference.com. As a researcher in semantics and etymology, Tayyaba's passion for the complexity of languages and their distinctions has found a perfect home on the platform. Tayyaba delves into the intricacies of language, distinguishing between commonly confused words and phrases, thereby providing clarity for readers worldwide.