Computer Concept & Programming In C - Unit IV - 13

Q.26. Write a program using pointer to read in an array of integers and print its elements in reverse order.                                                (AKTU. 2011 - 12) 
Ans. # include<stdio.h>
# include<canio.h>
# include<malloc.h>
void main ( )
{
    int *p;
    int i, n;
   clrscr ( );
   printf(“\n Enter number of elements in array”);
  scanf (“%d”&n);
  p = (int*)malloc(n*size of (int));
        if (p == NULL)
  printf(“\n Required memory unavailable”);
        else
          {
  printf(“\n Enter the elements”);
for (i=0; i<n; i++)
    scanf(“%d”,(p+i));
printf(“\n Elements in reverse order”);
      for (i=n; i>=0; i--)
    printf(“\n%d”,*(p+i));
        }
getch( );
           }

Q.27 Write short notes of the following :
(i) Dynamic memory allocation in ‘C’  (ii) Creation of linked list in ‘C’.
Related Questions -
Q. What are the merits and demerits of static and dynamic memory allocation techniques?                                                          (AKTU. 2009-10)
Q. What is dynamic memory allocation?                 (AKTU. 2011 - 12)
Q. Write a short note on the dynamic memory allocation in C. How it is  done ? Explain with suitable examples.                                                         (AKTU. 2012 - 13)
Ans. (i) Dynamic Memory Allocations in ‘C’: -
The concept of dynamic or run-time memory allocation helps us to get the required chunk of memory at run-time or we can say as the need arises. This is best suited type of allocation where we do not know the memory requirement in advance which is the case with most real-life problems.
‘C’ provides the following dynamic allocation and de-allocation functions :
(i) malloc ( ) (ii) calloc ( ) (iii) free ( ) (iv) realloc ( )
(i) The Malloc ( ) Function: -
The malloc ( ) function allocates a block of memory in bytes. The user should explicitly give the block size it requires for use. The malloc ( ) function is like a request to RAM of the system to allocate memory, if the request is granted (i.e. the malloc function stays successful in allocating memory), returns a pointer to the first block of memory. The type of pointer it returns is void, which means that we can 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 the header file <alloc.h> or <stdlib.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 * size of (int))
Where size represents 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 ( ) 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 form :
ptr_var = (type_cast *) malloc (size)
Where ptr_var is the name of pointer that holds the starting address of allocated memory block, type_cast isthe data type into which the returned pointer (or type void) is to be converted, and size specifies the size of allocated memory block in bytes.
For example,
int ptr;
ptr = (int *) malloc (10 * size of (int));
After the execution of this statement, a consecutive memory block of 20 bytes (size of integer is 2 bytes, and 10 * 2 = 20 bytes) is allocated to the program. The address of first byte of this block is first casted into int and then assigned to the pointer ptr. Consider another example :
char * ptr;
ptr = (char *) malloc (10 * size of (char));
After the execution of the above statement, a memory block of 10 bytes (10 * size of (char)) which is equivalent to 10 * 1 = 10, as size of char is 1 byte) is allocated to the program. The starting address of this block is assigned to ptr.
Similarily for allocation of memory to a structure variable, the following statements are required :
struct student
{
int roll_no;
char name [30];
float percentage;
};
struct student * st_ptr;
This is what is called declaration of structure, for allocation of memory the following statement is given :