SlideShare a Scribd company logo
1 of 20
 Reserves memory from a much larger free
 store, or heap

 Gives the programmer a pointer to refer to
 this memory
There are two types of memory management
 operators in C++:

 new
 delete


 These two memory management operators
 are used for allocating and freeing memory
 blocks in efficient and convenient ways.
The new operator in C++ is used for dynamic

storage allocation. This operator can be used

to create object of any type.
The general syntax of new operator in C++ is
as follows:

pointer variable = new datatype;

In the above statement, new is a keyword
and the pointer variable is a variable of type
datatype.
For example:
               int *a=new int;
 The new operator allocates sufficient
 memory to hold the object of datatype int
 and returns a pointer to its starting point.
 The pointer variable a holds the address of
 memory space allocated.
Dynamic variables are never initialized by
the compiler. Therefore, the programmer
should make it a practice to first assign
them a value.
The assignment can be made in either of
the two ways:

int *a = new int;
*a = 20;

or

int *a = new int(20);
Dynamic memory allocation in case of
arrays can be done as following:

int* a = new int[x];

a points to a block of memory
containing x ints
The delete operator in C++ is used for
releasing memory space when the object
is no longer needed. Once a new operator
is used, it is efficient to use the
corresponding delete operator for release
of memory.
The general syntax of delete operator in
C++ is as follows:

delete pointer_variable;

In the above example, delete is a keyword
and the pointer_variable is the pointer
that points to the objects already created
in the new operator.
 The programmer must take care not to
 free or delete a pointer variable that has
 already been deleted.

 Overloading of new and delete operator is
 possible (to be discussed in detail in later
 section on overloading).
 We know that sizeof operator is used for
 computing the size of the object. Using
 memory management operator, the size
 of the object is automatically computed.

 The programmer must take care not to
 free or delete pointer variables that have
 not been allocated using a new operator.
 Null pointer is returned by the new
 operator when there is insufficient
 memory available for allocation.
#include <iostream.h>
  void main()
  {//Allocates using new operator memory
  space //in memory for storing a integer
  datatype
  int *a= new a;
  *a=100;
  cout << " The Output is:a="<<a;
  //Memory Released using delete operator
  delete a;
The Output is:a=100
The constructors can also be used to allocate
memory while creating objects. This will
enable the system to allocate the right
amount of memory for each object when the
objects are not of the same size, thus
resulting in the saving of memory. Allocation
of memory to objects at the time of their
construction is known as dynamic
construction of objects. The memory is
allocated with the help of
new operator
# include <iostream.h>
 # include <conio.h>
# include <string.h>
class str
{
char *name;
int len;
public:
str()
{
len=0;
name=new char[len+1];
 }
str(char *s)
 {
len=strlen(s);
 name=newchar[len+1];
 strcpy(name,s);
 }
void show()
{ cout<<"NAME IS:->"<<name<<endl;
 }
void join(str &a,str &b);
};
void str::join(str &a,str &b)
{
    len=a.len+b.len;
    delete name;
    name=newchar[len+1];
    strcpy(name,a.name);
    strcat(name,b.name);
 };
void main()
{
   clrscr();
   char *first="HARSHIL";
   str n1(first), n2("NINAD"), n3("PRATIK"), n4, n5;
   n4.join(n1,n2);
   n5.join(n4,n3);
   n1.show();
   n2.show();
   n3.show();
   n4.show();
   n5.show();
}

More Related Content

What's hot

Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
Programming the cloud with Skywriting
Programming the cloud with SkywritingProgramming the cloud with Skywriting
Programming the cloud with SkywritingDerek Murray
 
Scilab - Piecewise Functions
Scilab - Piecewise FunctionsScilab - Piecewise Functions
Scilab - Piecewise FunctionsJorge Jasso
 
Part II: 2-Dimensional Array file name: lab1part2.cpp (10 points) Write a C++...
Part II: 2-Dimensional Array file name: lab1part2.cpp (10 points) Write a C++...Part II: 2-Dimensional Array file name: lab1part2.cpp (10 points) Write a C++...
Part II: 2-Dimensional Array file name: lab1part2.cpp (10 points) Write a C++...hwbloom38
 
Python advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmPython advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmJohn(Qiang) Zhang
 
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With FunctionsMS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With FunctionsDataminingTools Inc
 
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012Wen-Wei Liao
 
Ds
DsDs
DsAcad
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeData Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeRsquared Academy
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures LavanyaJ28
 
Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in MatlabJorge Jasso
 
The Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterateThe Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iteratePhilip Schwarz
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresJohn(Qiang) Zhang
 

What's hot (19)

Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Programming the cloud with Skywriting
Programming the cloud with SkywritingProgramming the cloud with Skywriting
Programming the cloud with Skywriting
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
 
Scilab - Piecewise Functions
Scilab - Piecewise FunctionsScilab - Piecewise Functions
Scilab - Piecewise Functions
 
Part II: 2-Dimensional Array file name: lab1part2.cpp (10 points) Write a C++...
Part II: 2-Dimensional Array file name: lab1part2.cpp (10 points) Write a C++...Part II: 2-Dimensional Array file name: lab1part2.cpp (10 points) Write a C++...
Part II: 2-Dimensional Array file name: lab1part2.cpp (10 points) Write a C++...
 
Python advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmPython advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithm
 
WAP to add two given matrices in Java
WAP to add two given matrices in JavaWAP to add two given matrices in Java
WAP to add two given matrices in Java
 
hash
 hash hash
hash
 
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With FunctionsMS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
 
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012
 
Ds
DsDs
Ds
 
Matlab numbers
Matlab numbersMatlab numbers
Matlab numbers
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeData Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
 
List
ListList
List
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in Matlab
 
The Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterateThe Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterate
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
 

Viewers also liked

Vietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android AppsVietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android AppsGameLandVN
 
Memory management for_android_apps
Memory management for_android_appsMemory management for_android_apps
Memory management for_android_appsBin Shao
 
[Vietnam Mobile Day 2013] - Memory management for android applications
[Vietnam Mobile Day 2013] - Memory management for android applications[Vietnam Mobile Day 2013] - Memory management for android applications
[Vietnam Mobile Day 2013] - Memory management for android applicationsAiTi Education
 
Memory management in Android
Memory management in AndroidMemory management in Android
Memory management in AndroidKeyhan Asghari
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it worksMindfire Solutions
 

Viewers also liked (11)

Calculating garbage value in case of overflow
Calculating garbage value in case of overflowCalculating garbage value in case of overflow
Calculating garbage value in case of overflow
 
Vietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android AppsVietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android Apps
 
Memory management for_android_apps
Memory management for_android_appsMemory management for_android_apps
Memory management for_android_apps
 
[Vietnam Mobile Day 2013] - Memory management for android applications
[Vietnam Mobile Day 2013] - Memory management for android applications[Vietnam Mobile Day 2013] - Memory management for android applications
[Vietnam Mobile Day 2013] - Memory management for android applications
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Gc in android
Gc in androidGc in android
Gc in android
 
Memory management in Android
Memory management in AndroidMemory management in Android
Memory management in Android
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
 
Memory management
Memory managementMemory management
Memory management
 
Memory management
Memory managementMemory management
Memory management
 
Memory management
Memory managementMemory management
Memory management
 

Similar to 16858 memory management2

ADK COLEGE.pptx
ADK COLEGE.pptxADK COLEGE.pptx
ADK COLEGE.pptxAshirwad2
 
C++ MEMORY MANAGEMENT.pptx/ kbibuvvw veovn nveknev ovne onv
C++ MEMORY MANAGEMENT.pptx/ kbibuvvw veovn nveknev ovne onvC++ MEMORY MANAGEMENT.pptx/ kbibuvvw veovn nveknev ovne onv
C++ MEMORY MANAGEMENT.pptx/ kbibuvvw veovn nveknev ovne onvChetanRaut43
 
Memory management
Memory managementMemory management
Memory managementsana younas
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Sameer Rathoud
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndEdward Chen
 
I really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdfI really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdfwasemanivytreenrco51
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
(3) cpp abstractions more_on_user_defined_types
(3) cpp abstractions more_on_user_defined_types(3) cpp abstractions more_on_user_defined_types
(3) cpp abstractions more_on_user_defined_typesNico Ludwig
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
PF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptxPF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptxhelpme43
 
I am trying to fill out a program where the method definitions will b.docx
I am trying  to fill out a program where the method definitions will b.docxI am trying  to fill out a program where the method definitions will b.docx
I am trying to fill out a program where the method definitions will b.docxPhil4IDBrownh
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 

Similar to 16858 memory management2 (20)

ADK COLEGE.pptx
ADK COLEGE.pptxADK COLEGE.pptx
ADK COLEGE.pptx
 
C++ MEMORY MANAGEMENT.pptx/ kbibuvvw veovn nveknev ovne onv
C++ MEMORY MANAGEMENT.pptx/ kbibuvvw veovn nveknev ovne onvC++ MEMORY MANAGEMENT.pptx/ kbibuvvw veovn nveknev ovne onv
C++ MEMORY MANAGEMENT.pptx/ kbibuvvw veovn nveknev ovne onv
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Memory management
Memory managementMemory management
Memory management
 
PPT DMA.pptx
PPT  DMA.pptxPPT  DMA.pptx
PPT DMA.pptx
 
memory
memorymemory
memory
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
 
I really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdfI really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdf
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
Pointer
PointerPointer
Pointer
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
(3) cpp abstractions more_on_user_defined_types
(3) cpp abstractions more_on_user_defined_types(3) cpp abstractions more_on_user_defined_types
(3) cpp abstractions more_on_user_defined_types
 
8 Pointers
8 Pointers8 Pointers
8 Pointers
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
PF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptxPF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptx
 
Pointers
PointersPointers
Pointers
 
I am trying to fill out a program where the method definitions will b.docx
I am trying  to fill out a program where the method definitions will b.docxI am trying  to fill out a program where the method definitions will b.docx
I am trying to fill out a program where the method definitions will b.docx
 
Operators
OperatorsOperators
Operators
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 

16858 memory management2

  • 1.
  • 2.  Reserves memory from a much larger free store, or heap  Gives the programmer a pointer to refer to this memory
  • 3. There are two types of memory management operators in C++:  new  delete These two memory management operators are used for allocating and freeing memory blocks in efficient and convenient ways.
  • 4. The new operator in C++ is used for dynamic storage allocation. This operator can be used to create object of any type.
  • 5. The general syntax of new operator in C++ is as follows: pointer variable = new datatype; In the above statement, new is a keyword and the pointer variable is a variable of type datatype.
  • 6. For example: int *a=new int; The new operator allocates sufficient memory to hold the object of datatype int and returns a pointer to its starting point. The pointer variable a holds the address of memory space allocated.
  • 7. Dynamic variables are never initialized by the compiler. Therefore, the programmer should make it a practice to first assign them a value.
  • 8. The assignment can be made in either of the two ways: int *a = new int; *a = 20; or int *a = new int(20);
  • 9. Dynamic memory allocation in case of arrays can be done as following: int* a = new int[x]; a points to a block of memory containing x ints
  • 10. The delete operator in C++ is used for releasing memory space when the object is no longer needed. Once a new operator is used, it is efficient to use the corresponding delete operator for release of memory.
  • 11. The general syntax of delete operator in C++ is as follows: delete pointer_variable; In the above example, delete is a keyword and the pointer_variable is the pointer that points to the objects already created in the new operator.
  • 12.  The programmer must take care not to free or delete a pointer variable that has already been deleted.  Overloading of new and delete operator is possible (to be discussed in detail in later section on overloading).
  • 13.  We know that sizeof operator is used for computing the size of the object. Using memory management operator, the size of the object is automatically computed.  The programmer must take care not to free or delete pointer variables that have not been allocated using a new operator.
  • 14.  Null pointer is returned by the new operator when there is insufficient memory available for allocation.
  • 15. #include <iostream.h> void main() {//Allocates using new operator memory space //in memory for storing a integer datatype int *a= new a; *a=100; cout << " The Output is:a="<<a; //Memory Released using delete operator delete a;
  • 17. The constructors can also be used to allocate memory while creating objects. This will enable the system to allocate the right amount of memory for each object when the objects are not of the same size, thus resulting in the saving of memory. Allocation of memory to objects at the time of their construction is known as dynamic construction of objects. The memory is allocated with the help of new operator
  • 18. # include <iostream.h> # include <conio.h> # include <string.h> class str { char *name; int len; public: str() { len=0; name=new char[len+1]; } str(char *s) { len=strlen(s); name=newchar[len+1]; strcpy(name,s); } void show() { cout<<"NAME IS:->"<<name<<endl; } void join(str &a,str &b); };
  • 19. void str::join(str &a,str &b) { len=a.len+b.len; delete name; name=newchar[len+1]; strcpy(name,a.name); strcat(name,b.name); };
  • 20. void main() { clrscr(); char *first="HARSHIL"; str n1(first), n2("NINAD"), n3("PRATIK"), n4, n5; n4.join(n1,n2); n5.join(n4,n3); n1.show(); n2.show(); n3.show(); n4.show(); n5.show(); }