malloc() vs new Last Updated : 08 Jul, 2021 Comments Improve Suggest changes 179 Likes Like Report Following are the differences between malloc() and operator new.: Calling Constructors: new calls constructors, while malloc() does not. In fact primitive data types (char, int, float.. etc) can also be initialized with new. For example, below program prints 10. CPP #include<iostream> using namespace std; int main() { // Initialization with new() int *n = new int(10); cout << *n; getchar(); return 0; } Output: 10 2. operator vs function: new is an operator, while malloc() is a function. 3. return type: new returns exact data type, while malloc() returns void *. 4. Failure Condition: On failure, malloc() returns NULL where as new throws bad_alloc exception. 5. Memory: In case of new, memory is allocated from free store where as in malloc() memory allocation is done from heap. 6. Size: Required size of memory is calculated by compiler for new, where as we have to manually calculate size for malloc(). 7. Buffer Size: malloc() allows to change the size of buffer using realloc() while new doesn't newmalloc()calls constructordoes not calls constructors It is an operatorIt is a functionReturns exact data typeReturns void *on failure, Throws bad_alloc exception On failure, returns NULLsize is calculated by compilersize is calculated manually Create Quiz Comment K kartik Follow 179 Improve K kartik Follow 179 Improve Article Tags : C++ Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like