facebook like button

07 June, 2011

Dynamic memory allocation

What is Dynamic memory allocation ???
     You may or may not have heard the term. Don't worry about this much. In programming jargon whenever the memory is allocated during execution of the program rather than compile time or load time of the program is termed as dynamic memory allocation. Now question comes in which cases memory is allocated at load time? Answer is till now upto previous post of this blog wherever memory was allocated, was allocated at load time.


Why dynamic memory allocation ???
     Suppose you have to make program to manipulate some data and which may be of long range. I mean sometimes the data to be stored can be of some units and sometimes it can be go up to millions. If the memory is allocated at load time, on each run of program we have to declare an array of size more than the maximum expected size of input data. Now suppose you have taken declared array of 10million locations and data in that program run is merely some units, this is absolute wastage of resources (available memory). To get rid of this problem dynamic memory allocation is used.


How?
    For this we have some built-in functions. There are three function to allocate memory dynamically.
1. malloc => allocates a block oh memory
2. calloc => allocates n blocks of memory
3. realloc => modifies a previous allocation of memory.
Out of all these malloc is most commonly used. I don't think I have ever used calloc or realloc unless it was asked to do so. So I'll elaborate only malloc rest you can find on google.


The malloc() function in C:
Like I have already said that malloc() is used to allocate a block of memory of specified size and type and returns its starting address to a pointer variable of same type. How this is done is shown in below given sample code.
int *p;
p=(int *)malloc(200);
This statement allocates a continuous block of 200bytes and returns a pointer to it to variable integer pointer variable p. This is also a way to initialise a pointer variable. If memory is not successfully allocated malloc function returns NULL to p.


Additional functions:
Some additional functions also should be known to us when we write programs using dynamic memory allocation.
1. free() => to free the memory allocated by previous allocation. The syntax is:
free(p);
this frees the memory occupied by p so that after the use of p operating system can use that space for some other purpose.


2. sizeof(): The sizeof() function in C returns the size of datatype passed to it in integer value which can also be assigned to a variable. The syntax is:
int size;
size=sizeof(float);
This assigns value 4 to variable size because float data-type takes 4 bytes in the memory. In this way this function can be used to find out which data-type takes how many bytes in memory whether it is built-in or user defined data-type.
    This also feciliate writing memory allocation statement because the programmer need not bother about the size of memory used. Say I want to allocate memory for 100 integers. I also want to tell you that in unix compilers int takes 2bytes while in C-free3.5 or higher it takes 4bytes. So the programmer who does not know this fact about C-free will make mistake of writing
p=(int *)malloc(200);
which will allocate 200bytes which is sufficient to hold only 50 integers instead of 100 in C-free. So he may face problem later in the program.
To do away with this blunder he can write the statement in the following way:
p=(int *)malloc(100*sizeof(int));
This first calculate size of an integer for that particular compiler then multiplies by 100 to get the appropriate value of apace and allocate that. 

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply