Memory Management operators

Wednesday, 10 July 2013

MEMORY MANAGEMNT OPERATOR

Need for Memory Management operators

The concept of arrays has a block of memory reserved. The disadvantage with the concept of arrays is that the programmer must know, while programming, the size of memory to be allocated in addition to the array size remaining constant.
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
What are 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.
New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be used to create object of any type.
General syntax of new operator in C++:
The general syntax of new operator in C++ is as follows:
Pointer variable = new data type;
In the above statement, new is a keyword and the pointer variable is a variable of type data type.
For example:
Int *a=new int;
In the above example, the new operator allocates sufficient memory to hold the object of data type 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);


Delete operator:
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.
General syntax of delete operator in C++:
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. Some of the important points the programmer must note while using memory management operators are described below:

  • 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 size of 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.
Example: to understand the concept of new and delete memory management operator in C++:


#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 of the above program is
The Output is: a=100
In the above program, the statement:
Int *a= new a;
Holds memory space in memory for storing a integer data type. 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 as:
Delete a;
C++ Dereference Operator
It is possible to access the value of variables pointed by the pointer variables using pointer. This is performed by using the Dereference operator in C++ which has the notation *.
The general syntax of the Dereference operator is as follows:
*pointer variable
In this example, pointer variable denotes the variable defined as pointer. The * placed before the pointer variable denotes the value pointed by the pointer variable.
For example:
Item = 100;
test = item;
x = &item;
y=*x;
In the above example, the assignment takes place as below:


That is


In the above example, the item is a integer variable having the value of 100 stored in memory address location 3501.
The variable item is assigned to the variable test in the second statement:
Test = item;
The value of the variable item is 100 and is then copied to the variable test.
In the third statement, the address of the variable item denoted by reference operator &item is assigned to the variable x as:
x = &item;
The address of the variable 3501 and not the contents of the variable item is then copied into the variable x.
The fourth statement makes use of the deference operator:
y=*x
This means that the value pointed to by the pointer variable x gives the value 100 to y.
Some points for the programmer to note:

  • The programmer must note that the x refers to the address 3501 whereas *x refers to the value stored in the address 3501 namely 100.
    .
  • The reference operator is denoted by & and deference operator denoted by *. Both differ in their meaning and functionality. The reference operator denotes the address of. The dereference operator denotes the value pointed by. In short, a deference variable can be denoted as referenced.
    .
  • If the programmer wants to define more than two pointer variables, then comma operator may be used in this instance. The programmer must carefully place pointer symbol * before each pointer variable.
For instance, if the user wishes to define two integer pointer variables, e1 and e2, this can be done as follows:
Int *e1,*e2;
If the programmer declares:
Int *e1, e2;
This means that e1 is a pointer variable pointing to integer data type but e2 is only an integer data type and not a pointer type. The programmer must ensure to place * before each pointer variable.
The dereferencing or indirect addressing is performed using the indirection operator * used to access the value stored in an address.
The defining of pointer variable:
int* exf;


The definition of pointer variable as in the above case is the pointer variable exf.

It is also possible to assign value to pointer variable using indirection operator. This gives the same effect as working with variables.

For Example:


#include
void main()
{
int example, test;
int* item;
item=&example;
example=200;
test=200;
*item=100;
test=*item;
cout< cout< }

The output of the above program is

100
100
In the above example, the pointer variable item points to integer variable example and takes the address of the variable example as:


Test and example have initial values of 200.
The statement:
*item=100
Sets the value of the variable pointed by pointer variable item as 100. This is equivalent to setting example=100.
The assignment is as follows:
Example test


The statement

Test=*item;
Sets the value of variable test with the value pointed by the pointer variable item which is 100 as seen in the above example. Test also becomes 100 and the assignment becomes:


Both results are displayed having the value 100.
Defining pointers and using them to access the variable pointed by them is an important concept thoroughly detailed in this tutorial. In the next section, the powerful concept of pointers with other features of C++ such as arrays and functions will be explained in detail.

No comments:

Post a Comment