Difference Between new and malloc() in C++
new is an operator that allocates memory AND calls the constructor; malloc() is a C function that only allocates raw bytes. Full table plus delete vs free.
The flat analogy
Student *s1 = (Student*) malloc(sizeof(Student)); // raw bytes only!
// constructor NOT called
// name, marks = garbage
Student *s2 = new Student("Aman", 92); // memory + constructor
// object fully ready
Watch the difference run
#include <iostream>
#include <cstdlib>
using namespace std;
class Student {
public:
string name;
Student() { name = "Ready"; cout << "Constructor ran\n"; }
~Student() { cout << "Destructor ran\n"; }
};
int main() {
cout << "-- new --\n";
Student *a = new Student(); // constructor runs
delete a; // destructor runs
cout << "-- malloc --\n";
Student *b = (Student*) malloc(sizeof(Student)); // silence... nothing
free(b); // silence again
return 0;
}
See it clearly: with malloc/free, no constructor, no destructor — the object was never properly born, never properly cleaned. For a class holding a string, file or connection, that means corruption and leaks.
Full comparison table
| Point | new | malloc() |
|---|---|---|
| What it is | C++ operator | C library function (<cstdlib>) |
| Calls constructor | ✅ Yes | ❌ Never |
| Return type | Correct pointer type | void* — cast required in C++ |
| Size calculation | Automatic | You write sizeof() yourself |
| On failure | Throws bad_alloc exception | Returns NULL (must check manually) |
| Partner for cleanup | delete (calls destructor) | free() (just releases bytes) |
| Can be overloaded | ✅ Yes | ❌ No |
The pairing rule — never mix them
int *p = new int[10];
free(p); // WRONG: undefined behaviour!
int *q = (int*) malloc(10 * sizeof(int));
delete[] q; // WRONG: undefined behaviour!
// Correct pairs, always:
new <-> delete
new[] <-> delete[]
malloc <-> free
new[] must go to delete[] (with brackets), or only the first element's destructor runs.Bottom line
- In C++ code, use new/delete (or better, smart pointers / vectors) — malloc has no place in normal C++ class code because it skips constructors.
- malloc appears in C++ only when interfacing with C libraries or doing low-level raw-buffer work.
- Interview line: "new = allocation + construction, throws on failure, type-safe; malloc = raw allocation only, returns void*, NULL on failure."
Flat वाली analogy
Student *s1 = (Student*) malloc(sizeof(Student)); // सिर्फ raw bytes!
// constructor NAHI चला
// name, marks = garbage
Student *s2 = new Student("Aman", 92); // memory + constructor
// object पूरी तरह तैयार
फर्क को चलते हुए देखिए
#include <iostream>
#include <cstdlib>
using namespace std;
class Student {
public:
string name;
Student() { name = "Ready"; cout << "Constructor ran\n"; }
~Student() { cout << "Destructor ran\n"; }
};
int main() {
cout << "-- new --\n";
Student *a = new Student(); // constructor चला
delete a; // destructor चला
cout << "-- malloc --\n";
Student *b = (Student*) malloc(sizeof(Student)); // सन्नाटा... कुछ नहीं
free(b); // फिर सन्नाटा
return 0;
}
साफ दिखा: malloc/free के साथ न constructor, न destructor — object कभी ठीक से पैदा ही नहीं हुआ, कभी ठीक से साफ नहीं हुआ. String, file या connection रखने वाली class के लिए इसका मतलब corruption और leaks.
पूरी comparison table
| Point | new | malloc() |
|---|---|---|
| क्या है | C++ operator | C library function (<cstdlib>) |
| Constructor call | ✅ हां | ❌ कभी नहीं |
| Return type | सही pointer type | void* — C++ में cast ज़रूरी |
| Size calculation | Automatic | sizeof() खुद लिखना पड़ता है |
| Fail होने पर | bad_alloc exception throw | NULL return (खुद check करो) |
| Cleanup partner | delete (destructor call करता है) | free() (सिर्फ bytes छोड़ता है) |
| Overload हो सकता है | ✅ हां | ❌ नहीं |
Pairing rule — कभी mix न करें
int *p = new int[10];
free(p); // गलत: undefined behaviour!
int *q = (int*) malloc(10 * sizeof(int));
delete[] q; // गलत: undefined behaviour!
// सही जोड़े, हमेशा:
new <-> delete
new[] <-> delete[]
malloc <-> free
new[] की memory delete[] (brackets के साथ) में ही जाए, वरना सिर्फ पहले element का destructor चलता है.निचोड़
- C++ code में new/delete use करें (या और बेहतर, smart pointers / vectors) — normal C++ class code में malloc की कोई जगह नहीं क्योंकि वह constructors skip करता है.
- malloc C++ में सिर्फ तब आता है जब C libraries से interface करना हो या low-level raw-buffer काम हो.
- Interview line: "new = allocation + construction, fail पर throw, type-safe; malloc = सिर्फ raw allocation, void* return, fail पर NULL."
Frequently Asked Questions
What is the main difference between new and malloc?
new allocates memory AND calls the constructor, returning a typed pointer and throwing bad_alloc on failure; malloc only allocates raw bytes, returns void*, gives NULL on failure and never calls constructors.
Can we free memory allocated with new?
No. Memory from new must be released with delete, new[] with delete[], and malloc with free — mixing the families is undefined behaviour.
Why does C++ code prefer new over malloc?
Because objects need their constructors to run to be valid; malloc skips construction and returns uninitialized raw memory, corrupting classes that hold strings, files or other resources.