Interview Question

new/delete vs malloc/free?

new/delete manage object lifetime; malloc/free manage raw storage.

💡 Concept ✅ Quick Revision 🧠 C++

Answer

new expressions create typed objects and delete expressions destroy objects created by matching new expressions. • new performs allocation and initialization; delete performs destruction and deallocation. • malloc and free manage raw storage and do not call C++ constructors or destructors. • Do not mix new with free or malloc with delete; prefer RAII containers and smart pointers.

💡 C++ Example

auto value = std::make_unique<int>(42); void* storage = std::malloc(sizeof(int)); std::free(storage);

⚡ Quick Revision

new/delete manage object lifetime; malloc/free manage raw storage.