Sie sind auf Seite 1von 8

Need for Memory Management operators In programming there may be scenarios where programmers may not know the

memory needed until run time. In this case, the programmer can opt to reserve as much memory as possible, assigning the maximum memory space needed to tackle this situation. This would result in wastage of unused memory spaces. Memory management operators are used to handle this situation in C++ programming language.

There are two types of memory management operators in C++: 1) new 2) delete These two memory management operators are used for allocating and freeing memory blocks in efficient and convenient ways. 1) New operator: a) The new operator in C++ is used for dynamic storage allocation. This operator can be used to create object of any type. b) new is followed by a data type specifier and if a sequence of more than one element is required- the number of these within brackets [ ]. It returns a pointer to the beginning of the new block of memory allocated. c)General syntax of new operator in C++: pointer variable = new datatype ; In the above statement, new is a keyword and the pointer variable is a variable of type datatype. Example 1: int *a = new int ; In the above example, 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. There are actually two things that happen--memory allocation and object construction; the new keyword is responsible for both. One step in the process is to call operator new in order to allocate memory; the other step is to actually invoke the constructor.

d) The assignment can be made in either of the two ways: int *a = new int; *a = 20; or int *a = new int(20);

2)Delete operator: a) The delete operator in C++ is used for releasing memory space when the object is no longer needed. b)Once a new operator is used, it is efficient to use the corresponding delete operator for release of memory. c)General syntax of delete operator in C++: delete pointer variable; Example: To understand the concept of new and delete memory management operator in C++: Sample Code #include <iostream.h> #include<conio.h> void main( ) { //Allocates using new operator memory space in memory for storing a integer datatype int *a= new int; *a=100; cout << " The Output is:a= " << *a; //Memory Released using delete operator delete a;

getch( ); }

In the above program, the statement: int *a= new a; Holds memory space in memory for storing a integer datatype. The statement: *a=100 This denotes that the value present in address location pointed by the pointer variable a is 100 and this value of a is printed in the output statement giving the output shown in the example above. The memory allocated by the new operator for storing the integer variable pointed by a is released using the delete operator

REFERENCE VARIABLE
a) C++ introduces new kind of variable called as the reference variable. b) This variable provides an alternative name for a previously defined variable. c) Syntax is:datatype & reference name=variable-name; Case1:For ex:int a=20; int &b = a; cout<<a; cout<<b; // both a and b prints the value as 20 ,as both are referring to the same data object in the memory

Case2:int a=20; int &b = a; b=b+2; cout<<b; cout<<a; // both a and b prints the value as 22 ,as both are referring to the same data object in the memory

Case3:int a=20; int &b = a;

a=a*2; cout<<b; cout<<a; // both a and b prints the value as 40 ,as both are referring to the same data object in the memory

COPY CONSTRUCTOR EXAMPLE


#include<iostream.h> #include<conio.h> class A { int cost,quantity; public: A(int x,int y) { cost=x; quantity=y; } A(A &ob1) { cost=ob1.cost; quantity=ob1.quantity; cout<<"copy constructor is called"; } void put() { cout<<cost<<endl<<quantity<<endl; } }; main() //copy constructor, here &ob1 is the reference variable. //parametrized construcor

{ clrscr(); A a(4,5); // calling the parametrized constructor a.put(); A c(a); // calling the copy constructor // a is the existing object and c is the new object. // The value of a is copied to c using copy constructor.

c.put(); getch(); }

OUTPUT

4 5

//values 4 and 5 are printed on screen when parameterized constructor is called by the object a

copy constructor is called 4 5 //values 4 and 5 are printed on screen when copy constructor is called by the object c

Das könnte Ihnen auch gefallen