new/delete vs malloc/free

Difficulty: Beginner

Question

What is the difference between new/delete and malloc/free? What goes wrong if you mix them, or use delete instead of delete[]?

Answer

This is the bread-and-butter memory question in C++ interviews, and the trick is to go beyond 'new is an operator and malloc is a function'. Let's talk about what really happens.

malloc is a C library function. You give it a byte count, it returns a void* pointing to raw, uninitialised memory, or NULL on failure. It knows nothing about types. free simply releases that block. new is a C++ operator that does two jobs: it calls operator new to allocate raw memory, then runs the constructor of the type in that memory. Correspondingly, delete first runs the destructor and then calls operator delete to release the memory. That constructor and destructor call is the essential difference; for a class that owns resources, skipping it means the object is never properly built or torn down.

Other differences follow from this. new returns a correctly typed pointer, so no cast is needed, while malloc returns void* which C++ needs cast. On failure, new throws std::bad_alloc (unless you use the nothrow form), while malloc returns NULL, so you need to check it. new computes the size for you from the type, whereas with malloc you multiply by sizeof yourself and can get it wrong. new and delete can be overloaded per class or globally; malloc and free cannot. And malloc offers realloc for resizing, which has no new equivalent; that is one reason vector exists.

Now the pairing rules, which are the real interview meat. Memory from new must be released with delete, memory from new[] with delete[], and memory from malloc with free. Mixing them is undefined behaviour. Using plain delete on an array allocated with new[] typically fails to run all the element destructors and may corrupt the heap, because the allocator often stores an element count that only delete[] reads. Similarly, calling delete twice on the same pointer is a double free, also undefined behaviour, while delete on a nullptr is harmless and defined as a no-op.

An analogy: malloc is renting an empty flat, and new is renting a flat that is already furnished and inspected. delete is not just returning the keys, it first clears out the furniture properly.

Mention placement new too: 'new (buffer) T(args)' constructs an object in already-allocated memory, which is how containers like vector construct elements inside spare capacity. And close with modern practice: in application code you should almost never write raw new or delete. Use std::make_unique, std::make_shared, and containers so that ownership is automatic.

Code examples

Constructors run with new, not with malloc

#include <cstdlib>
#include <iostream>
#include <new>

struct T {
    int v;
    T() : v(7) { std::cout << "ctor\n"; }
    ~T() { std::cout << "dtor\n"; }
};

int main() {
    T* a = new T;           // allocate + construct
    std::cout << a->v << "\n";
    delete a;               // destroy + deallocate

    void* raw = std::malloc(sizeof(T));   // raw bytes only, no ctor
    T* b = new (raw) T;     // placement new: construct in existing memory
    b->~T();                // must destroy manually
    std::free(raw);

    int* arr = new int[3]{1, 2, 3};
    std::cout << arr[2] << "\n";
    delete[] arr;           // delete[] for new[]
}

Plain malloc never runs the constructor; placement new does. The array form of new must be paired with delete[].

Key points

Common follow-up questions

Interview tips

Concepts covered

new, delete, malloc, free, constructors

More C++ & STL questions