Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · C++ · 04 Jul 2026 · Hindi + English

C++ में new और malloc() में अंतर

new एक operator है जो memory allocate करके constructor भी call करता है; malloc() C का function है जो सिर्फ raw bytes देता है. पूरी table और delete vs free.

The flat analogy

malloc() gives you an empty plot of land — raw space, nothing built, you must set up everything yourself. new gives you a ready-to-move-in flat — the space is allocated AND the constructor has already run: furniture placed, electricity connected. That single difference — constructor call — is why new exists.
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;
}
-- new -- Constructor ran Destructor ran -- malloc --

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

Pointnewmalloc()
What it isC++ operatorC library function (<cstdlib>)
Calls constructor✅ Yes❌ Never
Return typeCorrect pointer typevoid* — cast required in C++
Size calculationAutomaticYou write sizeof() yourself
On failureThrows bad_alloc exceptionReturns NULL (must check manually)
Partner for cleanupdelete (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
Mixing allocator families is undefined behaviour — the program may work today and crash in production. Also remember the array pair: memory from 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

malloc() आपको खाली plot देता है — कच्ची जगह, कुछ बना नहीं, सब खुद set करना है. new आपको ready-to-move-in flat देता है — जगह allocate हुई AND constructor भी चल चुका: furniture लगा, बिजली connected. यही एक अंतर — constructor call — new के होने की वजह है.
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;
}
-- new -- Constructor ran Destructor ran -- malloc --

साफ दिखा: malloc/free के साथ न constructor, न destructor — object कभी ठीक से पैदा ही नहीं हुआ, कभी ठीक से साफ नहीं हुआ. String, file या connection रखने वाली class के लिए इसका मतलब corruption और leaks.

पूरी comparison table

Pointnewmalloc()
क्या हैC++ operatorC library function (<cstdlib>)
Constructor call✅ हां❌ कभी नहीं
Return typeसही pointer typevoid* — C++ में cast ज़रूरी
Size calculationAutomaticsizeof() खुद लिखना पड़ता है
Fail होने परbad_alloc exception throwNULL return (खुद check करो)
Cleanup partnerdelete (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
Allocator families mix करना undefined behaviour है — program आज चलेगा और production में crash करेगा. Array pair भी याद रखें: 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 हो जाती हैं.