Computer Concept & Programming In C - Unit IV - 14

st_ptr = (struct student *) malloc (size of (struct student));
When this statement is executed, a contiguous block of memory of size 36 bytes (2 bytes for integer, 30 bytes for char type name, and 4 bytes for float percentage).
Note that to check that if the request made through the function malloc ( ) to allocate the memory is granted by system RAM, or rejected in case if required space is not available), we can make use the fact that if the required amount of memory is not allocated, the memory function returns a NULL. This can be done as follows :
int * ptr;
ptr = (int *) malloc (5 * size of (int));
if (ptr = = NULL)
{
printf (“\n required memory not available”);
getch ( );
exit (0);
}
Example: -
To demonstrate the use of dynamic memory allocation function malloc ( ). This program finds the sum and average of all elements of the array.
# include <stdio.h>
# include <conio.h>
void main ( )
{
int * ptr;
int i, n, sum = 0;
float avg;
clrscr ( );
printf (“\n enter number of elements you want to store in an array”);
scanf (“% d”, & n);
ptr = (int *) malloc (n * size of (int));
if (ptr = = NULL)
{
printf (“\n required memory unavailable”);
}
else
{
printf (“\n enter the elements”);
for (i = 0; i < n; i + +)
sum = sum + (* (ptr + i));
printf (“\n sum of % d elements of array is = % d”, n, sum);
avg = sum / n;
printf (“\n the average of % d number of the array is % f”, n, avg);
}
getch ( );
}
(ii) The Calloc ( ) Function: -
The function works exactly similar to malloc ( ) function except for that 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 byte for which we want the allocation to be made, which in this case is 2 for integers. And 10 specify the number of 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 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 zeros. The calloc ( ) function is also available in the header file <stdlib.h> or <alloc.h> in TURBO C.
(iii) The Free ( ) Function: -
The free ( ) function is used to de-allocate the previously allocated memory using malloc ( ) or calloc ( ) functions.
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 return the allocated memory to the system RAM.
(iv) The Realloc ( ) Function: -
This function is used to resize the size of the memory block, which is already allocated (i.e. to modify the size of the already allocated memory block. It is found use of in two situations :
(i) If the allocated memory block is in sufficient for current application.
(ii) 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. The syntax of this function is as follows :
ptr_var = realloc (ptr_var, new_size);
Where ptr - var is the pointer holding the starting address of already allocated memory block. And new-size is the size in bytes you want the system to allocated memory block or may be greater than the size of previously allocated memory block depending upon the requirement. This function is also available in the header file <stdlib.h>. Note that before using the realloc ( ) function, the memory block to be resized should be allocated, using the following statement;
ptr - var = malloc (size);
Program illustrates the use of functions free ( ) and realloc ( )
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main ( )
{
char * msg;
msg = (char *) malloc (30 * size of (char));
strcpy (msg, “I am able”);
printf (“\n the message is now % s\n”, msg);
msg = (char *) realloc (msg, 50);
stropy (msg, “I saw you yesterday”);
printf (“\n the message is now % s\n”, msg);
free (msg);
getch ( );
}
(ii) Creation of Linked List in ‘C’: -
Linked lists are special list of some data elements linked to one another. The logical ordering is represented by having each element pointing to the next element. Each element is called a node, which has two parts. INFO part which stores the information and POINTER which points to the next element.
Basically, we can put linked lists into the following four types :
(1) A Singly Linked List: -
Is one in which all nodes are linked together in some sequential manner. Hence it is also called linear linked list.

(2) A doubly Linked List: -
Is one in which all nodes are linked together by multiple links which help in accessing both the success or node (next node) and predecessor node (previous node) for any arbitrary node within the list.

(3) A Circular Linked List: -
Is one which has no beginning and no end. A singly linked list can be made a circular linked list by simply storing the address of the first node in the link field of the last node.


(4) A circular Doubly Linked List: -
Is one which has both successor pointer and predecessor pointer in circular manner.