C++ में new और malloc() में अंतर
new एक operator है जो memory allocate करके constructor भी call करता है; malloc() C का function है जो सिर्फ raw bytes देता है. पूरी table और 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
new और malloc में main अंतर क्या है?
new memory allocate करके constructor भी call करता है, typed pointer देता है और fail पर bad_alloc throw करता है; malloc सिर्फ raw bytes देता है, void* return करता है, fail पर NULL और constructor कभी नहीं call करता.
क्या new की memory को free() से छोड़ सकते हैं?
नहीं. new की memory delete से, new[] की delete[] से, और malloc की free से release होनी चाहिए — families mix करना undefined behaviour है.
C++ code में malloc की जगह new क्यों prefer होता है?
क्योंकि objects को valid होने के लिए constructor चलना ज़रूरी है; malloc construction skip करके uninitialized raw memory देता है, जिससे strings, files या resources रखने वाली classes corrupt हो जाती हैं.