Suppose you are making a C++ program to store a particular number (specified by the user at runtime) of phone numbers. What will you do? You cannot declare array of 10 or 20 elements because you don’t know how many numbers the user will enter. He may want to enter 1, 10 or even 100 phone numbers.
So what will you do?
Actually, you can do two things to achieve this, that are listed below:
-
You can create a very large int(eger) array (say, of 500 elements) , in this case whether the user wants to store 1 or 500 phone numbers, the memory used will always be the same (large!).
-
You can make use of C++’s dynamic memory allocation to allocate only the needed amount of memory.
The first choice is a very bad way of programming, nevertheless it works fine, you should never employ such an inefficient method of programming.
So, you are left with only one choice to use the Dynamic Memory Allocation of C++ programming language, that I will be discussing in this article.
As is clear from the above example, dynamic memory allocation is needed when you don’t know the program’s memory needs beforehand.
Allocating Memory
Dynamic memory is allocated by the new keyword. Memory for one variable is allocated as below:
ptr=new DataType (initializer);
Here,
-
ptr is a valid pointer of type DataType.
-
DataType is any valid c++ data type.
-
Initializer (optional) if given, the newly allocated variable is initialized to that value.
Ex.
//Example Program in C++
#include<iostream.h>
void main(void)
{
int *ptr;
ptr=new int(10);
cout<<*ptr;
delete ptr;
}
This is will allocate memory for an int(eger) having initial value 10, pointed by the ptr pointer.
Memory space for arrays is allocated as shown below:
ptr=new DataType [x];
Here,
-
ptr and DataType have the same meaning as above.
-
x is the number of elements and C is a constant.
Ex.
//Example Program in C++
#include<iostream.h>
void main(void)
{
int *ptr, size;
cin>>size;
ptr=new int[size];
//arrays are freed-up like this
delete []ptr;
}
Freeing-Up Allocated Memory
Unlike static variables, c++ will not free-up the memory allocated through dynamic allocation. Its your duty to free them up. delete keyword is used to free-up the allocated memory.
delete ptr;
Arrays are deleted in a slightly different manner as shown below:
delete []ptr;
It’s easy to forget to free the allocated memory because C++ compiler won’t inform you that you are doing this. It’s your job and should always be done.
Now let me show you an example of dynamic memory allocation in action:
//Example Program in C++
#include<iostream.h>
void main(void)
{
int *ptr, size;
cout<<"How many PH. Numbers do you wish to enter:";
cin>>size;
ptr=new int[size];//allocate memory
//input ph. numbers
for (int i=0;i<size;i++)
{
cout<<"Enter PH. NO."<<i+1<<" :";
cin>>ptr[i];
}
//output ph. numbers
cout<<"\n\n\n PH. NOs. are\n";
for (i=0;i<size;i++)
cout<<"\nPH. NO."<<i+1<<" :"<<ptr[i];
cout<<endl;
delete []ptr;//free-up memory
}
Good-Bye!
Related Articles:
Ok but what if you want to use a two dimensional array, allocation would look something like
ReplyDeleteint width(3), height(5);
dataType** myArray=NULL;
myArray=new dataType[width];
for(int i=0;i less than width;++i)
{
myArray[i]=new dataType[height];
}
But what would deletion look like?
I thought it should be the reverse:
for(int i=0;i less than width;++i)
{
delete [] myArray[i];
myArray[i]=NULL;
}
delete [] myArray;
myArray=NULL;
Now I have tried this and it didn't work, although my 2D array is of type dataType* so it is in effect a 3D array and so I have an extra for() loop for that.
Thanx for asking
ReplyDeleteFirst of all since myArray is a pointer to a pointer, you should allocate it like
myArray=new dataType *[width];
which means allocating an array of pointers.
your deallocating part seems correct to me, YOU DON'T NEED to use muArray=NULL though.
This comment has been removed by a blog administrator.
ReplyDeleteYou teach good!
ReplyDeleteThe language you use is quite simple.
And I understand the dynamic memory allocation very well.
Hi maria,
ReplyDeleteThanks very much!
Enjoy.
Hi thanks.. but i what to know the dynamic memory allocation for two dimensional array... will u please help me.....
ReplyDeleteHi
ReplyDeleteYou cannot directly allocate multi-dimensional arrays. I've seen a nice tutorial on how to do this:
http://www.geocities.com/varunhostel/TechnicalArticles/PointerArticle/PointerArticle_Part2.html?reload_coolmenus#pos9
Its really good man...
ReplyDeleteHi Anonymous,
ReplyDeleteThanks! :-)
can anyone explain about copy constructor in c++?
ReplyDeletePretty good explanation
ReplyDeletegreat helped alot been trying to get a handle on how to understand this your great dude!
ReplyDeletei understand about dynamic memory allocation so i wanna to say you thank you very very much keep in touch
ReplyDeletevery nice. i appreciate you.
ReplyDeleteplz give me a code for adding two matrices using dynamic memory allocation
ReplyDeleteAwesome logic!
ReplyDeleteIntroduction letter template
thankyou verymuch...
ReplyDeletethanksssssssss
ReplyDeletethank you so much it really helped me understand dynamic memory allocation =)
ReplyDelete