Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Memory Management operators

 
Category: C++
Comments (6)

C++ Memory Management operators

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 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;


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.


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 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.

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 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 as:


delete a;



Read Next: C++ Dereference Operator



 

 

Comments


Justin said:

  int *a= new a;

should be

int *a = new int;


The new operator accepts types, not variable names.
October 25, 2007, 2:48 am

aruperez said:

  Reading what it is exposed before that new is the statement that allocates memory blocks, makes me wonder about all the other situations if you should worry about releasing the memory used for example:
If you have a subroutine like this

int subroutine(int param){
char example_variable [200];

whatever the routine does....

}

is necessary to release the memory used by the variable example_variable??? or is this automatically released when the variable get out of scope???

I guess that what i'm trying to ask is: do i only have to worry for the objects that i create using the new statement???

AR
August 4, 2008, 11:09 am

aruperez said:

  After reading this Article, there is some situations that makes me wonder how's is a correct memory management. Let's say in a routine inside your code, you have some variables such as this;
int routine(int param){
char example[200];
...}do i have to worry about the liberation of the memory used by the variable example? or it is released automatically once the variable get out of scope?i guess what i'm trying to ask is , the only memory i have to worry to release is the one used by the variables created using the new statement?
August 4, 2008, 11:28 am

Jass Singh said:

  plz send show the veriable address in c++
October 20, 2008, 6:00 am

abhishek said:

  variables are created in different spaces within a program.
the example[100] variable would be created within a stack which would be deleted as soon as the program terminates so the programmer doesn't need to take care of freeing that. but any address dynamically obtained is obtained from the heap which is a pool of memory of all the programs running and not specific to one. so a programmer needs to take care of freeing or deleting (basically returning the space back to the pool) when the purpose of that variable is served and no longer required.
June 13, 2009, 8:02 am

ansa said:

  what does the delete operator do in addition to deallocation of memory used by object
October 6, 2009, 3:44 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape