C++ Interview Question

What is the difference between new and malloc in C++?

Updated 2026-07-10 · Beginner friendly
Quick answer

new is a C++ operator that allocates memory and calls the object's constructor, returns a correctly typed pointer, and is paired with delete. malloc is a C function that only allocates raw memory, does not call constructors, returns a void pointer you must cast, and is paired with free. In C++ you should prefer new, or better still smart pointers.

Key differences

int* a = new int(5);   // allocates and initialises
delete a;

int* b = (int*)malloc(sizeof(int));  // raw memory, no init
free(b);
In the interview

The modern answer is you rarely use either directly in good C++, because smart pointers like unique_ptr manage memory for you. Mentioning that shows you follow current best practice, not just the textbook difference.

Want the full C++ guide?

Read every C++ concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the C++ guide All C++ questions