JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

C++ : New and Delete Command in hindi , what is new and delete command in c++ language program example

what is new and delete command in c++ language program example , C++ : New and Delete Command in hindi :-
इससे पहले के article मे , pointer के basic को discuss किया है | लेकिन इस article मे new और delete command को discuss करेगे जिसका use pointer variable के साथ ही किया जा सकता है |
Memory Allocation With New
इससे पहले pointer variable को declare करना देखा अब इस article मे methodology of pointer variable को discuss करते है |सबसे पहले Oops का use dynamic memory allocation मे use किया जाता है |dynamic memory allocation मे, pointer variable को declare करता है | इस pointer variable से run time मे memory को allocate करता है | और प्रत्येक pointer , किसी न किसी memory location से connect होता है | pointer variable कस use किसी unnamed memory location को use किया जाता है |
C language मे , malloc () function का use dynamic memory allocation के लिए use किया जाता है | In
C++ मे , new operator का use ,dynamic memory allocation के लिए किया जाता है |
new operator से एक unnamed memory space को create किया जाता है जिसका data type integer होता है
|pointer variable का use इस memory space को access करने एक लिए किया जाता है |new operator से memory space को create किया जाता है जिसका memory location को pointer variable मे assign किया जाता है | उदाहरण केलिए :
int *p= new int ;
यह पर int ये define करता है की एक memory space है जिसमे integer type value assign होती है | और new operator के द्वारा allocate memory space के द्वारा occupy bit size को भी define करता है |और इस memory space के address को pointer variable’p’ मे assign कर देता है |
एक pointer assignment को और भी briefly discuss करेगे  :-
int add;
int *pt= &add;
दोनों ही उदाहरण के लिए , p और pt मे integer data type की address को store करता है | लेकिन दुसरे उदहारण मे , integer variable ‘add’ के memory address को store किया जाता है | जब किसी memory space जिसका type integer है को इस method से भी assign किया जाता है |
तो new operator की क्या जरुरत होती है | pointer variable ‘p’ data object को point करता है | data object किसी variable से अलग होता है | data object memory space होता है | किसी pointer variable से memory space / data object को control करना थोडा मुश्किल होता है लेकिन बाद मे इसके काफी advantages होते है |
New operator से data space को allocate का syntax निन्म होता है :
int pointer_name = new type name ;
इस declaration को हम दो तरह से कर सकते है :
1.Pointer variable को पहले declare कर देना और बाद मे इसे use करना |
2.Pointer variable को new statement के साथ declare करना |
इसका उदाहरन होगा :
#include<iostream.h>
#include<conio.h>
void main()
{
using namespace std ;
int *p=new int;
*p=1010;
cout<<“Value of Integer : “<<*p : << “Address is :” << p << endl;
double * pt = new double ;
*pd=1000.23 ;
cout<<“Value of Double : “<<*pd : << “Address is :” << pd << endl;
cout<<“Size of p :”<<sizeof(p);
cout<<“Size of *p :”<<sizeof(*p);
cout<<“Size of pd :”<<sizeof(pd);
cout<<“Size of *pd :”<<sizeof(pd);
getch();
}
इस उदहारण मे , दो memory block को declare किया जाता है | जिसमे एक का type integer ओत दुसरे का type double है | इसका आउटपुट होगा :
Value of Integer : 1010    Address is : 0x004301a6

 

 

Value of Double : 1000.23   Address is : 0x004301a8
Size of p : 4
Size of *p : 4
Size of pd : 4
Size of *pd : 8
Delete
new operator से किसी memory space को create किया जा सकता है उसी प्रकार delete option से किसी memory space को डिलीट किया जा सकता है | ये दोनों ही command C++ memory management package का भाग होता है | किसी प्रोग्राम को effective बनाने के लिए ये जरुरी है अगर किसी memory space को allocate किया जाता है तब उसी डिलीट करना भी जरुरी होता है | इसका syntax होता है :
delete pointer name ;
यहा पर
pointer name : ये उस pointer variable का नाम है जिसमे new operator से allocate memory space एक address को assign किया जाता है | अतः new operator और delete operator का एक जोड़ी होती है और दोनों साथ ही use किया जाता है |
delete statement से memory leak की प्रॉब्लम को solve किया जा सकता है |अगर memory seek की value बहुत बड़ी होती है तब किसी प्रोग्राम के द्वारा allocate की जनि space को search करना बहुत मुश्किल हो जाता है |
अगर किसी pointer variable से associate memory block को delete करने के बाद delete statement मे deleted pointer variable को use किया जाता है तब compiler error message देता है |
निन्म statement को भी discuss किया जाता है :
int *p= new int ; // valid, allocate a memory space of integer type
delete p; // delete a memory space that associated with pointer variable ‘p’
delete p; // invalid , because memory space that associated pointer variable ‘p’ is already delete
int a=6; // valid , Normal Assignment operator
int *p = &a; // valid
delete p ; // Not allowed , because ‘p’ did not contain memory space that allocate by new operator
इसका आउटपुट होगा :

 

#include<iostream.h>
#include<conio.h>
void main()
{
using namespace std ;
int *p=new int;
*p=1010;
cout<<“Value of Integer : “<<*p : << “Address is :” << p << endl;
double * pt = new double ;
*pd=1000.23 ;
cout<<“Value of Double : “<<*pd : << “Address is :” << pd << endl;
cout<<“Size of p :”<<sizeof(p);
cout<<“Size of pd :”<<sizeof(pd);
delete p;
delete pt;
getch();
}
इस उदाहरण मे दो delete statement है जिससे pointer variable ‘p’ और ‘pt’ से associated memory space को डिलीट किया जाता है |
इस article मे new और delete operation को discuss किया है |अब age के article मे newoperator के application को discuss करेगे |
Sbistudy

Recent Posts

मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi

malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…

4 weeks ago

कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए

राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…

4 weeks ago

हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained

hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…

4 weeks ago

तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second

Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…

4 weeks ago

चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi

chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…

1 month ago

भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi

first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…

1 month ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now