# new/delete vs malloc/free

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

Canonical: https://www.internhack.in/learn/interview/cpp-interview/cpp-new-delete-vs-malloc-free
Last updated: 2026-11-28

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

- new = allocate + construct; delete = destroy + deallocate. malloc/free do neither construction nor destruction.
- new throws std::bad_alloc on failure; malloc returns NULL.
- Match allocation and deallocation: new/delete, new\[\]/delete\[\], malloc/free.
- delete nullptr is safe; deleting the same pointer twice is undefined behaviour.
- Prefer make_unique, make_shared and containers over raw new/delete.

## Common follow-up questions

- What is placement new and when is it used?
- Can you call free on memory from new? What happens?
- What does operator new do differently from the new expression?

## Interview tips

- State the constructor/destructor difference first; it is the answer that separates you from memorised lists.
- Mention that raw new/delete is a code smell in modern C++.

## Concepts covered

new, delete, malloc, free, constructors

Previous: [Stack vs heap memory in C++](https://www.internhack.in/learn/interview/cpp-interview/cpp-stack-vs-heap) · Next: [Memory leaks, dangling pointers and double free](https://www.internhack.in/learn/interview/cpp-interview/cpp-memory-leaks-dangling-pointers) · [All of C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)

## More C++ & STL questions

- [RAII: Resource Acquisition Is Initialization](https://www.internhack.in/learn/interview/cpp-interview/cpp-raii)
- [Constructors in C++: default, copy and move](https://www.internhack.in/learn/interview/cpp-interview/cpp-constructors-copy-move)
- [Shallow copy vs deep copy](https://www.internhack.in/learn/interview/cpp-interview/cpp-shallow-vs-deep-copy)
- [Rule of three, rule of five and rule of zero](https://www.internhack.in/learn/interview/cpp-interview/cpp-rule-of-three-five-zero)
- [Lvalues, rvalues and move semantics](https://www.internhack.in/learn/interview/cpp-interview/cpp-lvalue-rvalue-move-semantics)
- [Copy elision, RVO and NRVO](https://www.internhack.in/learn/interview/cpp-interview/cpp-copy-elision-rvo)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)
