Top Ad unit 728 × 90

Memory Allocation in c

Memory Allocation in c


Memory Allocation in C:

There are two type of memory allocation in c:

  1.  compile-time or static allocation.
  2. Run-time or Dynamic allocation (using pointers).
In the first time of allocation, the required amount of memory is allocated to the program element ( identifier name which include variables name, function name, program name etc.) at the start of the program. Here the memory to be allocated to the variable is fixed and is determined by the compiler at the compile time (if it is a single integer variable it allocated two bytes to it, if it is an array of five integer values it allocates ten bytes to it and if it is a single float type of variable compiler allocates four bytes to it .). for example, consider the following declaration :

int  x, y ;
float  a[5];

When the first statement is encountered, the compiler will allocate two bytes each variables x and y.  The second statement result into the allocation of 20 bytes  to the array a(5*4, where there are five elements and each element of float type takes four bytes). Note that as there is no bound checking in C for array boundaries, i.e. if you have declared an array of five elements, as above and by mistake you are intending to read more then five values in the array a, it will still work without error. for example you are reading the above array as follows:

for (  i = 0; i < 10; i++ )
{
     scanf( " %d" , &a[i] );

Through you have declared size of array 5, you are trying to read ten elements. However the problem with this is that the extra elements added as a parts of this array "a" are not allocated the consecutive memory location after the five elements, i.e. only the first five elements  are store in consecutive memory location (as already told arrays are store in consecutive memory locations) and the other elements are store randomly at any unknown locations in the memory. Thus during accessing these extra elements would not be made available to the users, only the first five values can be accessible. 

The second problem with the static memory allocation is that if you store less number of elements than the number of elements for which you have declared memory, then the rest of the memory will be wasted (i.e., it is not made available to other applications and its status is set as allocated and not free). This leads to the inefficient use of memory. 

The concept of Dynamic or run-rime memory allocation helps us to overcome this problem in arrays, as well as allows us to be able to get the required chunk of memory at rum-time (or we can say as the needs arises).  this is best suited type of allocation where we do not know the memory requirement in advance, which is the case with most of real-life problems, In other words dynamic memory allocation gives flexibility for programmer. As well as it make efficient use of memory, by allocating the required amount of memory whenever needed, unlike static allocation where we declare the amount to be allocated statically.

C provides the following dynamic allocation and de-allocation functions:

1. Malloc ( ) 
2. Calloc ( )   
3. Free ( )   
4. Realloc ( )


1. Malloc ( ) :

The malloc() function allocates a block of memory in bytes. The users should explicitly give the block size it requires for the use. The Malloc () function is like a request to the RAM of the system to allocate memory, if the request is granted (i.e., the malloc () function stays successful in allocating  memory ), return a pointer to the first block of that memory. The type of  the pointer it returns is void, which means that we cab assign it any type of pointer However if the malloc () function fails to allocate the required amount of memory, it returns a NULL.  The malloc () function is available in header file alloc.h or stddlib.h in TURBO C, and on UNIX it will be available in header file <malloc.h>.  The syntax of this function is as follows:

Malloc (Number of elements  *  size of each element ) ;

for Example,
        int  *ptr ;
         ptr  =  malloc  ( 10 * sizeof(int)

Where the size represent the size of memory required in bytes (i.e., number of contiguous memory locations to be allocated). But as already told that the function malloc () function returns a void pointer so a cast operator is required to change the returned pointer type according to our need, the above declaration would take the following from:

ptr_var  = (type_cast * ) malloc (size)


The Calloc ():

This  function works exactly similar to malloc () function except for the fact that it needs two arguments as against one argument required by malloc ().

For example,
       int  *ptr
       ptr   =  (int * ) calloc ( 10 , 2);

 Here 2 specifies the size of data type in bytes for which we want the allocation to be made which in this case 2 for integers. And 10 specify the number off elements for which allocation  is to be made. Remember that the argument passed to the function malloc was (n *10) , it is a single argument (don't be confused) because multiple arguments are always separated by commas. the argument ( n* 10) has no commas in between hence it is a single argument,  though not a simple one but an expression. Returning to the above declaration after the execution of the above statement a memory block of 20 bytes is allocated to the requesting program and the address of the first block is assigned to pointer ptr. Another minor difference is that the memory allocated by malloc () function contains garbage values, while memory allocated by calloc () function contains all seros. the calloc () functio is also available in the head file <stdlib.h> or <alloc.h>  in TURBO C.


The Free ():

The free () function is used to de-allocate the previously allocated memory using malloc () for calloc () function. the syntax of this function is :

Free  (ptr_var)  ;

Where ptr_var is the pointer in which the address of the allocated memory block is assigned. the free function is used to rey=turn the allocated memory to the system RAM.

The Realloc ():

This function is used to resize the size of memory block, which is already allocated (i.e., to modify the size of already allocated memory block ). It found use of in two situations :
  1.  If the allocated memory block is sufficient for current application.
  2. If the allocated memory is much more than what is required by the current application. In other words it provides even more precise and efficient utilization of memory.
Syntax of this function is as follows :
       prt_var  =    realloc  (ptr_var , new_size );




Material science notes all units:

Unit-1: download pdf file: click here

Unit-2: download pdf file: click here

Unit-3: download pdf file: click here

Unit-4: download pdf file:  click here

Unit-1: download pdf file: click here


Memory Allocation in c Reviewed by For Learnig on March 14, 2023 Rating: 5

No comments:

If you have any doubts, please tell me know

Contact Form

Name

Email *

Message *

Powered by Blogger.