JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

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

Categories: c++ language in hindi

C++ : NEW Operator , Create Dynamic Arrays Using New Operator , what is NEW Operator in c++ language

Create Dynamic Arrays Using New Operator , what is NEW Operator in c++ language , C++ : NEW Operator :-
इससे पहले के article मे new operator और delete operator को discuss किया है | अब इस article मे new operator के application को discuss करगे |
Create Dynamic Arrays Using New Operator
जब किसी single variable को access करना होता तब single variable को define किया जाता है | जो की बहुत simple प्रोसेस होता है | अभी तक new operator से singel variable को declare करना को discuss किया था |
लेकिन जब new operator का use किसी array ,string और struture को define करने मे किया जाता है | इसलिए new operator बहुत उपयोगी होता है |
उदाहरण के लिए जब कसी प्रोग्रम मे array को डेफिन किया जाता या नहीं किया जाता है |new operator से डेफिन किया गया array हमेशा run time मे allocate होती है | अगर उस array को use किया जाता है या नहीं |फिर भी उस array के लिए allocate memory space यही पर रहता है |
किसी array को compile time मे allocate करना static binding कहलाता है | जिसे C++ : Array article मे discuss किया है | लेकिन new operator से array को run time मे allocate किया गया है |इसी dynamic binding कहलता है | इस तरह से define की गयी array को dynamic array कहते है | static binding  मे , array size को define करना होता है |और dynamic binding मे , array की size तब define होती जब प्रोग्राम को run किया जाता है |
Allocation of dynamic array
किसी dynamic array को declare करना बहुत सरल होता है | इसका syntax होता है :
int *p= new int [10];
यहा पर
int : ये integer type को define करता है |
p : pointer variable है जिसमे memory block का address assign होता है |
new : ये keyword है जिससे new operator को call किया जाता है |
[size] : ये array की size को define करता है |
new operator के द्वारा , pointer variable मे dynamic array के first element के address को assign करता है | और किसी new operator को use करने के बाद delete operator को भी use करना जरुरी होता है | अतः dynamic array के लिए delete statement का syntax निन्म होता है :-
delete [] p;
यहा पर :
delete : ये keyword है जिसे delete operator के लिए use किया जाता है |
[] : ये dynamic array के लिए use किया जाता है |
p: ये pointer variable का नाम है जिसमे dynamic array के memory space का address store होता है |
[] हमेशा delete और pointer name के बीच होना चाहिए | अगर प्रोग्राम मे new operator को [] के साथ use किया जाता है तब delete operator को भी [] के साथ use किया जाता है | अगर किसी delete operator को use नहीं किया गया है तब erroneous आउटपुट आ सकता है |
इसका rules होता है :
1.delete operator का use किसी memory space को delete करने के लिए नहीं किया जाता है अगर new operator का use नहीं है |
2.किसी delete operator का use दो बार नहीं हो सकता है |
3.अगर new[] है तो delete[] ही होगा |
4.अगर new operator का use single entity के लिए किया जाता है तब delete operator को use , [ ] के बिना होगा |
C++ compiler मे,dynamic array के लिए elements को ट्रैक करना का कार्य manually करना होता है | क्योकि C++ compiler ,dynamic array के elements को ट्रैक नहीं करता है |उपर वाले उदाहरण के लिए , pointer variable ‘p’ मे , array के first element के address को assign करता है |
उदाहरण के लिए :
#include<iostream.h>
#include<conio.h>
void main()
{
using namespace std ;
int *p=new int [3];
p[0]=2;
p[1]=12;
p[2]=123;
cout<<“Value of Integer : “<<*p : << “Address is :” << p << endl;
double * pt = new double ;
cout<<“Value of p[0] : “<<p[0] : << endl;
cout<<“Value of p[1] : “<<p[1] : << endl;
cout<<“Value of p[2] : “<<p[2] : << endl;
p[2]=p[2]+p[1];
cout<<“Value of p[2] : “<<p[2] : << endl;
delete [ ] p;
getch();
}
इस उदाहरण मे , एक dynamic array को create किया गया है | जिसका type integer और first element के address को pointer variable ‘p’ मे assign किया गया है | अतः array के किसी element को pointer variable से access करने के लिए indexing method को use करते है | उदाहरण केलिए :
p[0] : इस syntax से array के first element को access किया जाता है |
p[1] : इस syntax से array के second element को access किया जाता है |
p[2] : इस syntax से array के third element को access किया जाता है |
Index हमेशा ‘0’ से start होता है size-1 तक चलता है |
प्रोग्राम मे अत मे , delete operator से memory space को free कर दिया गया |
इसका आउटपुट होगा :

Value of p[0] :2
Value of p[1] :12
Value of p[2] :123

Increment और Decrements in pointer variable
जिस प्रकार C language मे , किसी pointer variable मे increment और decreament करने पर इसके स्थित address की value मे increment और decrements होगा | और ये increment और decrements  , define data type के size से होता है उसी प्रकार C++ मे , increment और decrements  operation हो सकता है | इसके उदाहरण के लिए :
#include<iostream.h>
#include<conio.h>
void main()
{
using namespace std ;
int a =112,b=223;
int *p= &a;
cout<<“Value of Integer Pointer Variable (p): ” <<*p : << “Address is :” << &p << endl;
int *p1=&b;
cout<<“Value of Integer Pointer Variable (p1): ” <<*p1 : << “Address is :” << &p1 << endl;
*p=*p+1;
cout<<“Value of *p+1 :”<<*p<<endl;
cout<<“Address that store into *p+1 : “<<p<< endl;
getch();
}
इस उदाहरण मे increment को discuss किया है सबसे पहले दो pointer variable ‘p’ और ‘p1’ को declare किया गया है | पहले pointer variable मे variable ‘a’ के address को point करता है | और pointer variable ‘p1’ मे variable ‘b’ के address को point करता है |
जब pointer variable ‘p’  को increment किया जाता है तब ये variable ‘b’ को memory address को point करता है |
क्योकि b को ‘a’ के बाद declare किया जाता है और *p+1 से *p मे store मे स्थित address मे 4 bits से increment हो जाता है | इसका आउटपुट होगा :
Value of Integer Pointer Variable (p): 112   Address is : 4428
Value of Integer Pointer Variable (p): 223   Address is : 4432
Value of *p+1 : 223
Address that store into *p+1 : 4432
Pointer Increment और Decrements का use dynamic array के elements को access करने के लिए किया जाता है | इसका उदाहरण निन्म होता है :
#include<iostream.h>
#include<conio.h>
void main()
{
using namespace std ;
int *p=new int [3];
p[0]=2;
p[1]=12;
p[2]=123;
cout<<“Value of First Element :”<<*p<<endl;
*p=*p++;
cout<<“Value of Second Element :”<<*p<<endl;
*p=*p++;
cout<<“Value of Third Element :”<<*p<<endl;
delete [ ] p;
getch();
}
इस उदाहरण मे
पहले cout statement से , pointer variable मे स्थित address के value को display किया जाता है | ये value array की first element होता है |
उसके बाद *p=*p++; से pointer variable मे store address की value मे increment होगा जिससे ये pointer variable array के दुसरे element के address को point करता है |
दुसरे cout statement से , second element display होगा |
उसके बाद *p=*p++; से pointer variable मे store address की value मे increment होगा जिससे ये pointer variable array के तिरसी element के address को point करता है |
दुसरे cout statement से , third element display होगा |
इसका आउटपुट होगा :

Value of First Element :2
Value of Second Element :12
Value of Third Element :123

इस article मे , new operator से dynamic array को create , access करने के method को discuss किया है | article मे C++ : Pointer Operation मे , pointer से basic mathematical operation के प्रोग्राम को discuss करेगे |
Sbistudy

Recent Posts

सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है

सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…

18 hours ago

मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the

marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…

18 hours ago

राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi

sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…

2 days ago

गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi

gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…

2 days ago

Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन

वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…

3 months ago

polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten

get all types and chapters polity notes pdf in hindi for upsc , SSC ,…

3 months 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