Dynamic Memory Allocation


Today’s topic that I chose is Dynamic Memory Allocation.

As we know, the very basic need of every program is memory allocation, initialization of variables etc. Most of the programs which we have made till now, we know about the size of the memory required. For eg. when we are building a normal calculator, we know that at maximum what we would require two variables for the input and one more variable for the output. Most of the times, these variables are required to be float (for easiness in giving results of calculations like division, sin, cos etc).

But as the level of the program increases, we see that we don’t know about the size of the input that would be enough. For eg. if we require to input a name, it would be enough to have a character array of a size of 25 or 30 characters, because mostly the names are of these many characters (but this doesn’t rule out the possibility of longer names). But if we want to implement a data structure for employees. Then we don’t know how many employees are their in the company (the number of people and their details keep on changing). So, their arises a need of dynamic memory allocation.

The main purpose of dynamic memory allocation is to efficiently use the memory space. For eg. Let their be a company A with 50 employees and a company B with 500 employees and we make a program for them. Now, let us assume that we have used arrays for our purpose of saving the data. Now, what should be the size of the array? If we take the size to be 100, then a memory space of 50 employees will get waster in the company A and similarly a memory space of 400 people will be required more in company B. But if we use dynamic memory allocation, no problem will occur as the memory space used will change according to the requirements.

I was recently searching for a way to dynamically allocate memory for arrays. I don’t like using malloc, calloc (I know many people would agree with me) and other such things. Though nothing good was found by me but something striked me. I thought of using functions for this purpose. So, I made a very simple program like this in dev-cpp:


#include<iostream>

#include<conio.h>

#include<math.h>

using namespace std;

void bin(int no,int bit){

int a[bit];

for(int i=0;i<bit;i++){

a[i]=no%2;

no/=2;

}

for(int i=bit-1;i>=0;i–){

cout<<a[i];

}

}

int main(){

int no,bit;

cout<<“This is a program to convert a number to it’s binary form in”;

cout<<“predefined number of bits:\n\n”;

cout<<“Enter the number:\n”;

cin>>no;

cout<<“Enter the number of bits:\n”;

cin>>bit;

if(pow(2,bit)>no){

bin(no,bit);

}

else{

cout<<“You entered an impossible task…”;

}

getch();

return 0;

}

For more info:

exforsys.com

cplusplus.com

Advertisements
Explore posts in the same categories: C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: