43. What Is Static Memory & Dynamic Memory Allocation?
Answer: Compiler allocates memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address is assigned to a pointer variable. This way of assigning pointer value to a pointer variable at compilation time is known as static memory allocation.
A dynamic memory allocation uses functions such as malloc() or calloc() to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these function are assigned to pointer variables, such a way of allocating memory at run time is known as dynamic memory allocation.
44. What Is Dynamic Memory Allocation? Explain The Various Memory Allocation Function With Its Task.
Answer: The mechanism of allocating required amount of memory at run time is called dynamic allocation of memory. Sometimes it is required to allocate memory at runtime. When we declare array in any program, the compiler allocates memory to hold the array.
Now suppose the numbers of items are larger than the defined size then it is not possible to hold all the elements and if we define an array large enough and data to be stored is less, in that case the allocated memory is wasted leading to a need for dynamic memory allocation. In this mechanism, we use a pointer variable to which memory is allocated at run time.
45. What Are The Three Dynamic Memory Allocation Functions?
Answer: Three dynamic memory allocation functions are: malloc, calloc and free.
46. Explain The Various Memory Allocation Function With Its Task In C Programming.
Answer: The various memory allocation functions are described below:
- malloc(): It is a memory allocation function that allocates requested size of bytes and returns a pointer to the first byte of the allocated space. The malloc function returns a pointer of type void so we can assign it to any type of pointer. It takes the following form:
where ptr is a pointer of type cast-type.
For example, the statement
x = (int *) malloc(10 *sizeof(int)) means that a memory space equivalent to 10 times the size of an int byte is reserved and the address of the first byte of memory allocated is assigned to the pointer x of int type.
The malloc function can also allocate space for complex data types such as structures. For example:
ptr= (struct student*) malloc(sizeof (struct student));
where ptr is a pointer of type struct student.
- calloc(): It is another memory allocation function that allocates space for an array of elements, initializes them to zero and then returns a pointer to the memory. This function is normally used for requesting memory space at run time. While malloc allocates a single block of storage space, calloc allocates multiple block of storage, each of the same size, and then sets all bytes to zero. It takes the following form:
This statement allocates contiguous space for n blocks, each of size element-size bytes.
- realloc(): realloc is a memory allocation function that modifies the size of previously allocated space. Sometime it may happen that the allocated memory space is larger than what is required or it is less than what is required. In both cases, we can change the memory size already allocated with the help of the realloc function known as reallocation of memory. For example, if the original allocation is done by statement
then reallocation is done by the statement
ptr = realloc(ptr,newsize); which will allocate a new memory space of size newsize to the pointer variable ptr and returns a pointer to the first byte of the new memory block.
47. What Are The Differences Between Malloc() & Calloc()?
Answer: Differences between malloc() and calloc() are:
- The malloc() function is used to allocate memory space. It reserves a memory space of specified size and gives the starting address to pointer variable whereas calloc() function is used to allocate multiple blocks of memory. This has two arguments. The calloc() function is generally used for allocating the memory space for arrays and structures.
- Malloc takes one argument Malloc(a);where a represents number of bytes whereas Calloc takes two arguments Calloc(b,c) where b is the no of object and c represents size of object.
- Calloc initializes the contains of block of memory to zeros whereas memory allocated using malloc contains garbage values.
- Memory allocated using Malloc malloc function is contiguous whereas memory allocated using calloc is not contiguous.
48. What Is The Purpose Of Realloc?
Answer: It increases or decreases the size of dynamically allocated array. The function realloc (ptr, n) uses two arguments. The first argument ptr is a pointer to a block of memory for which the size is to be altered.
The second argument specifies the new size. The size may be increased or decreased. If sufficient space is not available to the old region the function may create a new region.
49. What Is The Purpose Of The Free() Function?
Answer: For efficient use of memory space, the memory space that is not required should be released. The free() function achieves this.
50. How To Use Realloc() To Dynamically Increase Size Of An Already Allocated Array?
Answer: The function realloc() can be used to change the size of the memory block reserved for a particular object. This is known as reallocation of memory.
The function realloc() moves the content of old block into the new block and the data of the old block is not lost.
51. In Which Header File Are The Memory Management Functions Defined?
Answer: Memory management functions which are used for allocating and freeing memory during execution of a program. These functions are defined in stdlib.h.
52. How Much Maximum Memory Can Be Allocated In A Single Call To Malloc() ?
Answer: 64 KB
53. What Are The Differences Between Formal Arguments & Actual Arguments Of A Function?
Answer: Formal arguments are parameters or arguments in a function declaration. The scope of such arguments is local to the function definition. Any change made to the formal arguments will not be reflected back in the main program.
Actual arguments are arguments that are passed in a function call. Such arguments are defined inside the function body.
Check Out Other Questions:
36) What is the purpose of the free() function?
37) How to use realloc() to dynamically increase size of an already allocated array?
38) What is the purpose of the sizeof() operator?
39) What is a wild pointer in C?
40) What is the purpose of the do-while loop in C?
41) What is the size of the void pointer in C?
42) What is a nested loop in C?
43) What are the operations performed on C++ lists?
44) How is searching in an array list carried out?
45) Where can an element are inserted in an array list?
...Return To C Questions INDEX.
...Return To C Programming INDEX.
... Return To HR Interview Index