Q.1 What is static memory allocation?
Ans. When the memory is allocated during the compile time it is known as static memory allocation.

Q.2 What is dynamic memory allocation?
Ans. When the memory is allocated during the run time it is known as dynamic memory allocation.

Q.3 Write down the syntax of malloc() function?
Ans. The syntax of malloc() function is as:
(datatype *) malloc (n*sizeof(datatype));
Here n is the total number data items whose memory space is to be reserved during runtime.

Q.4 Write down the syntax of calloc() function?
Ans. The syntax of calloc() function is as:
(datatype *) calloc (n, sizeof(datatype));
Here n is the total number data items whose memory space is to be reserved during runtime.

Q.5 Write down the syntax of realloc() function?
Ans. The syntax of realloc() function is as:
(datatype *) realloc (ptr, n*sizeof(datatype));
Here ptr is a pointer to a block of memory for which the size is to be changed and n*sizeof(datatype) specifies the new size of the memory block.

Q.6 What is the use of free() function?
Ans. The free() function is used to release the memory space occupied by the function malloc() or calloc() function.

Q.7 Which operator is used to find out the number of bytes needed for a data type or a variable?
Ans. The sizeof()operator is used to find out the number of bytes needed for a data type or a variable

Q.8 How do you create a memory space for ‘n’ number of characters using malloc() function?
Ans.
char *ptr;
ptr = (char *)malloc(n*sizeof(char));

Q.9 What is the main difference between the malloc() and calloc() functions?
Ans. The malloc() function needs one argument whereas calloc() function needs two arguments, the number of data items and the size of each data item. Another difference is that calloc() function sets each element with a value 0 whereas in malloc() function each element is assigned a garbage value.

Q.10 Which function is used to free the memory space allocated by calloc() function?
Ans. The free() is used to free the memory space allocated by calloc() function.

Q.11 How would you dynamically allocate a memory space for ‘n’ number of integers?
Ans. A memory space for ‘n’ number of integers is allocated dynamically as:
int n, *ptr;
ptr = (int )malloc(n*sizeof(int));

Q.12 How would you dynamically allocate a memory space for a two-dimensional array of integers of dimensions m*n such that we are able to access any element using two subscript, as in a[i][j]?

Ans. int **a;
int m, n;
int i;

a = (int **)malloc(m*sizeof(int *);
for(i=0; i a[i] = (int )malloc(n*sizeof(int));

Q.13
Is there any other way of creating a two dimensional array of integers of dimensions m*n?
Ans. Yes. We can also create a two dimensional array of integers of dimensions m*n as:
int *a;
int m, n;
a = (int )malloc(m*n*sizeof(int));
In this case you can not access any element using two subscript as in a[i][j] rather you can access the same element using one subscript as: a[i*n+j].

Q.14 How would you dynamically allocate a memory space for a three-dimensional array of integers of dimensions m*n*k such that we are able to access any element using three subscript, as in a[i][j][k]?

Ans. int ***a;
int m, n, k;
int i, j;

a = (int ***)malloc(m*sizeof(int **);
for(i=0; i {
a[i] = (int )malloc(n*sizeof(int));
for(j=0; j {
a[i][j] = (int *)malloc(k*sizeof(int));
}
}

Q.15 How would you free the memory allocated for two-dimensional array as in Q.12?
Ans.     for(i=0; i free(a[i]);
free(a);

Q.16 How much maximum memory can malloc() function allocate in its single call?
Ans. 64KB

Q.17 What will happen if realloc() function is allowed to increase the allocated space for a 50 integer array to a 100 integers array?
Ans. If possible, the realloc() will increase the memory space at the same location at which the array is present; otherwise it would try to find a different place for the bigger array and returns the address of newly allocated space.

Q.18 How would you create a memory space dynamically for the following structure?
struct Employee
{
char name[20];
int age;
float salary;
};
Ans.
struct *eptr;
eptr = (struct Employee *)malloc(sizeof(struct Employee));

Q.19 How would you pass a one-dimensional array to a function?
Ans. main()
{
int a[10];
….
display(a, 10);
}
display(int a[], int n)
{
….
}
or

main()
{
int a[10];
….
display(a, 10);
}
display(int *a, int n)
{
….
}

Q.20 How would you pass a two-dimensional array to a function?
Ans. You can pass a two-dimensional array to a function in two ways:

(i)    main()
{
int a[5][5];
….
display(a);
}
display(int a[][5])
{
….
}
or

(ii)    main()
{
int a[5][5];
….
display(a);
}
display(int (*a)[5])
{
….
}


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments