Computer Concept & Programming In C - Unit IV - 12

Q.24 What do you mean by dynamic memory allocation.
Related Questions -
Q. What is dynamic memory allocation? Explain; malloc function with a suitable example.                                                                           (AKTU. 2008-09)
Q. What is dynamic memory allocation? Explain malloc function. (AKTU. 2009-10)
Ans. Dynamic Memory Allocation: -
The function malloc is used for memory allocation. It takes this general form:
malloc (number_of_memory_bytes);
The function malloc returns a pointer of type void, and you can use the cast operator to convert it to any type.
For example: name = (char*) malloc(20);
This allocates 20 bytes for the string pointed to by pointer “name”. The prototype of the function malloc is defined in the header file <alloc.h> (in TurboC and other Borland products), and in malloc.h (in Quick C or Microsoft products). It is defined also in <stdlib.h> in many compilers including those listed above.
If it fails to allocate the required memory, the function malloc returns a null pointer. You can check to see if the memory allocation has been successful by using an if statement.
Here is an example that receives a string from the keyboard and displays it on the screen. Eighty bytes of memory are allocated to string, and the check is made using the statement:
if ((string = (char*) malloc (80)) = =NULL){
printf (“\n Cannot allocate memory”);
return (1);
}
it is best to check the success of malloc whenever used.
# include <stdio.h>
# include <stdlib.h>
void main ( )
{
char *string;
if ((string = (char*)malloc(80)) = = NULL){
printf (“\n Cannot allocate memory”);
return (1);
}
printf (“\n Give me a string to display:”);
gets (string);
printf (“The input string is: %s\n”, string);
return (0);
}
The above program will not work without malloc function. To make the C program portable, many programmers use the size of operator along with the malloc as in the following statement, where a portion of memory is required to hold ten integers: pntr = (int*)malloc(10*size of (int));
If a specific machine is using a different integer size. This code will still work properly.

Q.25 Using dynamic memory allocation, write a program in C to accept element of a matrix of size 3 x 3 and print transpose of it.                                             (AKTU. 2010-11)
Related Questions
Q. Write a program in C to create a two dimensional integer array using the pointer and then display the sum of its major diagonal using pointer.   (AKTU. 2013-14)
Ans.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
 int *arr = NULL;
 int i, j;
 clrscr();
 arr = (int*)malloc(3 * 3 * sizeof(int));
 printf("\Enter the elements of the array");
 for( i = 0; i < 3; i++)
    for(j = 0; j < 3; j++)
       {
printf("\nEnter a[%d][%d] = ",i, j);
scanf("%d",(arr + (i * 3 * sizeof(int)) + j));
       };
 for( i = 0; i < 3; i++)
    for(j = 0; j < 3; j++)
       {
printf("\n a[%d][%d] = ",i, j);
printf("%d",*(arr + (i * 3 * sizeof(int)) + j));
       };
 printf("\n Transpose of the matrix is:\n");
 for( i = 0; i < 3; i++)
    for(j = 0; j < 3; j++)
       {
printf("\nTranspose a[%d][%d] = ",i, j);
printf("%d",*(arr + (j * 3 * sizeof(int)) + i));
       };
 getch();
}