What is the difference between new/delete and malloc/free in C++?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid C++ basics — a prerequisite for any developer role.
Answer
Both allocate and deallocate heap memory, but with critical differences: new/delete (C++): int* p = new int(42); // Allocates + initializes Car* c = new Car("Toyota", 2022); // Calls constructor! delete p; // Deallocates delete c; // Calls destructor then deallocates! int* arr = new int[10]; // Array allocation delete[] arr; // Must use delete[] for arrays!. Key: new calls the constructor; delete calls the destructor. Type-safe — no cast needed. Throws std::bad_alloc on failure (not null). malloc/free (C, inherited): int* p = (int*)malloc(sizeof(int)); // Manual cast required! *p = 42; // No initialization Car* c = (Car*)malloc(sizeof(Car)); // Does NOT call constructor! free(p); // Does NOT call destructor! free(c); // Destructor never runs -- resource leak!. Returns nullptr on failure. No type information — returns void*. Critical rule: never mix new/delete with malloc/free on the same allocation. Never use delete on something allocated with new[] (undefined behavior — use delete[]). Modern C++: avoid raw new/delete entirely — use std::make_unique<T> and std::make_shared<T> (smart pointers) for automatic memory management. They call constructors/destructors correctly and prevent leaks. auto c = std::make_unique<Car>("Toyota", 2022); // No manual delete needed
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last C++ project, I used this when...' immediately makes your answer more credible and memorable.
Previous
What are constructors and destructors in C++?
Next
What are pointers and references in C++?