0
0
CppComparisonBeginner · 3 min read

New vs malloc in C++: Key Differences and Usage

new is a C++ operator that allocates memory and calls constructors, while malloc is a C function that only allocates raw memory without initialization. new returns a typed pointer and throws exceptions on failure, whereas malloc returns a void pointer and returns nullptr on failure.
⚖️

Quick Comparison

This table summarizes the main differences between new and malloc in C++.

Aspectnewmalloc
Memory AllocationAllocates memory and calls constructorAllocates raw memory only
Return TypeTyped pointervoid* pointer (needs casting)
InitializationAutomatically initializes objectsNo initialization
Failure HandlingThrows std::bad_alloc exceptionReturns nullptr
DeallocationUse delete operatorUse free() function
LanguageC++ specificC standard library function
⚖️

Key Differences

new is a C++ operator designed for object-oriented programming. It allocates memory and then calls the constructor of the object, ensuring proper initialization. This means when you use new, your object is ready to use immediately after allocation.

On the other hand, malloc is a C library function that only allocates a block of raw memory. It does not call constructors or initialize the memory, so the allocated memory may contain garbage values. You must manually initialize the memory if needed.

Another important difference is type safety. new returns a pointer of the correct type, so no casting is needed. malloc returns a void*, which requires explicit casting to the desired pointer type in C++. Also, new throws an exception if allocation fails, making error handling easier, while malloc returns nullptr and requires manual checking.

⚖️

Code Comparison

Here is how you allocate and initialize an integer using new in C++:

cpp
int* ptr = new int(42);
std::cout << *ptr << std::endl;
delete ptr;
Output
42
↔️

malloc Equivalent

Here is the equivalent code using malloc. Notice you must cast the pointer and manually initialize the value:

cpp
#include <cstdlib>
#include <iostream>

int main() {
    int* ptr = (int*)malloc(sizeof(int));
    if (ptr == nullptr) {
        std::cerr << "Memory allocation failed" << std::endl;
        return 1;
    }
    *ptr = 42; // manual initialization
    std::cout << *ptr << std::endl;
    free(ptr);
    return 0;
}
Output
42
🎯

When to Use Which

Choose new when working in C++ because it handles object construction, type safety, and exceptions automatically, making your code safer and cleaner. Use malloc only when interfacing with C libraries or legacy code that requires it. Avoid mixing new with free() or malloc with delete to prevent undefined behavior.

Key Takeaways

new allocates and initializes objects with type safety and exception handling.
malloc only allocates raw memory and requires manual initialization and casting.
Always pair new with delete and malloc with free().
Prefer new in C++ for safer and cleaner memory management.
Use malloc mainly for C compatibility or legacy reasons.