Pointer and Dynamic memory Allocation in data structure through C Language

                                                            When the variables are declared in a program, usually the memory is allocated during the compilation that is statically. The memory referred by such variables is used to store the value (data) directly. Instead of data directly if another memory location is to be stored in the variables, then it should be a pointer variable. As we are already familiar with the dynamic memory allocation, and if it is to be implemented then without pointer variables it is not possible. During the execution of program dynamic memory is allocated, so there must be a variable that stores addresses of memory at that time. That is the reason why pointer variable is used. When pointer variables are declared no memory is allocated to store the data. The memory is allocated to them only to store the memory address not the data. Taking this as an advantage the pointer variable can be made to point dynamically allocated memory location.

For example if “p” is a pointer variable declared of the type “int” then for p memory is allocated to store the address of a memory is location that stores an “int” (integer data). Here the memory address that is to be stored in pointer variable may be static or dynamic. Usually it is to be dynamic memory allocation. It can be pictured as follows:
int *p, a;           ‘p’ is a pointer variable, ‘a’ is a simple integer variable. For ‘a’
                           memory is allocated statically to store integer data. Of course for                  
                           ‘p’ also memory is allocated but to store an address not data.
a=10;                 at memory location is reserved by ‘a’, an integer 10 is stored.
P=&a;                ‘p’ as it is pointer variable used to store address of statically
                           allocated memory.
With the help of pointer ‘p’ is possible to refer integer value stored at variable ‘a’ indirectly through pointer ’p’.

If ‘a’ is user to print, it prints the value 10, if &a (&a is called address operator) is used, it prints 1000 as per the examples. If p is used it also prints 1000 the content of p the address of memory is allocated to a. If *p (de-referencing of pointer) is used it prints the value of memory location pointed by the pointer variable. Here it prints the value of ‘a’ that is 10.
We can take the advantage of storing the memory address by a pointer variable during the execution of program. So instead of storing the already allocated static memory, a dynamic memory maybe requested during the execution of program and the dynamically allocated memory address may be stored in a pointer variable and it is used as it is normal variable.
Dynamically the memory is allocated using the standard functions calls depending on the high level languages used. Like in Pascal new used, in C++ new operator is used and in C language the functions like malloc(), calloc() and realloc() are used.
So, in C language if memory is requested by the user during the execution of the program to store data one of the following functions may be used. The functions are different as per the purpose and the numbers of arguments passed to the functions. All these functions return an address called the base address of dynamically allocated memory and is store in a pointer variable.

For example (int *) malloc (20) – allocates 20 bytes of consecutive memory dynamically on success, (or return a NULL address if not successfully allocates) and returns the base address. The returned address may be stored in a pointer variable of the type int and 10 integers can be referred with the help of that pointer variable. It is nothing but an array of size 10 for which the memory is allocated during the execution of program. The initial values stored in the memory location allocated are garbage values.

So, the memory allocated by the ‘calloc’ function is the product of number_of_elements and size_of_each_element. For example (int *) calloc (10, 2) allocates 20 bytes of dynamic memory. So, 10 integers can be stored in those memory locations. The initial values of these memory locations will be zero. Similar to malloc (), the returned address may be stored in a pointer variable and treated as an array of 10 integers.

If the dynamic memory allocated using either calloc or malloc is to be changed during the execution of program then realloc is used. It allocates completely a new memory block and copies the contents of already allocated block and returns the new base address. That address may be stored in the same pointer variable. Otherwise it expands the existing block and returns the same base address. The change may even reduce the size of already allocated block.
The dynamic memory is allocated from the heap area of main memory. If the requested memory is not available the dynamic memory is allocation functions return NULL address or invalid address, it is also treated as a 0 (zero) value. The dynamically allocated memory may be freed from the programmer after his/her usage using free () function. The prototype of void free (void *block) is available in alloc.h. If the user does not free the dynamically allocated memory, it is the function of operating system to collect the dynamic memory periodically and give it back to heap. This function of operating system called as “garbage collection”. It is better practice for the programmers to free the dynamically allocated memory after the use of such memory. If you write the statements for dynamic memory allocation then remember that the free statement is also used in the program before the end of program to ensure to free the dynamically allocated memory. 


Popular posts from this blog

difference between structure and union in C Language

Difference between Linear search and Binary Search in c language

Difference between static and dynamic websites