Quiz Entry - updated: 2026.07.14
What's the difference between new/delete in C++ and malloc/free in C?
new/delete call the object's constructor/destructor and are type-safe; malloc/free just (de)allocate raw bytes and do neither.
new/delete |
malloc/free |
|
|---|---|---|
| Type safety | Yes (returns the right pointer type) | No (returns void*) |
| Constructor | Called automatically | Not called |
| Destructor | Called automatically | Not called |
| Size | Computed for you | You pass the byte count |
| On failure | Throws std::bad_alloc |
Returns NULL |
// C++ way
Vehicle *v = new Vehicle(4); // constructor runs
delete v; // destructor runs
// C way (compiles, but wrong for a C++ class)
Vehicle *v = (Vehicle*)malloc(sizeof(Vehicle)); // no constructor!
free(v); // no destructor!
Tip: Never mix them — memory from new must be released with delete, and memory from malloc with free. Crossing them is undefined behaviour.
Go deeper:
new and delete — Wikipedia — why C++ adds these alongside C's malloc/free.