Memory Management
Dynamic Memory Allocation• Reserves memory from a much larger free store, or  heap• Gives the programmer a pointer to refe...
Memory management operators There are two types of memory management operators in C++:• new• delete These two memory manag...
New operatorThe new operator in C++ is used for dynamicstorage allocation. This operator can be used tocreate object of an...
New operatorThe general syntax of new operator in C++ is asfollows:pointer variable = new datatype;In the above statement,...
New operatorFor example:       int *a=new int; The new operator allocates sufficient memory to hold the object of datatype...
New operatorDynamic variables are never initialized by thecompiler. Therefore, the programmer shouldmake it a practice to ...
New operatorThe assignment can be made in either of the twoways:int *a = new int;*a = 20;orint *a = new int(20);
New operatorDynamic memory allocation in case of arrays canbe done as following:int* a = new int[x];a points to a block of...
Delete operatorThe delete operator in C++ is used for releasingmemory space when the object is no longerneeded. Once a new...
Delete operatorThe general syntax of delete operator in C++ is asfollows:delete pointer_variable;In the above example, del...
Important points while using memory         management operatorsThe programmer must take care not to free or delete a poi...
Important points while using memory         management operatorsWe know that sizeof operator is used for computing the si...
Important points while using memory         management operatorsNull pointer is returned by the new operator when there i...
Example:#include <iostream.h>  void main()  {//Allocates using new operator memory space //in  memory for storing a intege...
Output:The Output is:a=100
Dynamic ConstructorThe constructors can also be used to allocatememory while creating objects. This will enablethe system ...
# include <iostream.h> # include <conio.h># include <string.h>class str{char *name;int len;public:str(){len=0;name=new cha...
void str::join(str &a,str &b){    len=a.len+b.len;    delete name;    name=newchar[len+1];    strcpy(name,a.name);    strc...
void main() {   clrscr();   char *first="HARSHIL";   str n1(first), n2("NINAD"), n3("PRATIK"), n4, n5;   n4.join(n1,n2);  ...
Upcoming SlideShare
Loading in …5
×

16829 memory management2

292 views

Published on

memory management

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
292
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

16829 memory management2

  1. 1. Memory Management
  2. 2. Dynamic Memory Allocation• Reserves memory from a much larger free store, or heap• Gives the programmer a pointer to refer to this memory
  3. 3. Memory management operators 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. 4. New operatorThe new operator in C++ is used for dynamicstorage allocation. This operator can be used tocreate object of any type.
  5. 5. New operatorThe general syntax of new operator in C++ is asfollows:pointer variable = new datatype;In the above statement, new is a keyword and thepointer variable is a variable of type datatype.
  6. 6. New operatorFor 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. 7. New operatorDynamic variables are never initialized by thecompiler. Therefore, the programmer shouldmake it a practice to first assign them a value.
  8. 8. New operatorThe assignment can be made in either of the twoways:int *a = new int;*a = 20;orint *a = new int(20);
  9. 9. New operatorDynamic memory allocation in case of arrays canbe done as following:int* a = new int[x];a points to a block of memorycontaining x ints
  10. 10. Delete operatorThe delete operator in C++ is used for releasingmemory space when the object is no longerneeded. Once a new operator is used, it isefficient to use the corresponding deleteoperator for release of memory.
  11. 11. Delete operatorThe general syntax of delete operator in C++ is asfollows:delete pointer_variable;In the above example, delete is a keyword andthe pointer_variable is the pointer that points tothe objects already created in the new operator.
  12. 12. Important points while using memory management operators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. 13. Important points while using memory management operators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. 14. Important points while using memory management operatorsNull pointer is returned by the new operator when there is insufficient memory available for allocation.
  15. 15. Example:#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; }
  16. 16. Output:The Output is:a=100
  17. 17. Dynamic ConstructorThe constructors can also be used to allocatememory while creating objects. This will enablethe system to allocate the right amount of memoryfor each object when the objects are not of thesame size, thus resulting in the saving of memory.Allocation of memory to objects at the time oftheir construction is known as dynamicconstruction of objects. The memory is allocatedwith the help ofnew operator
  18. 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)
  19. 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. 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();

×