Memory Allocation is the process of the memory management in which we reserve some portion or complete part of computer memory space for various processes like execution of programs. It is a process which is used to assign the physical and virtual memory space of the computer to the programs and services of the computer. Lets have a look over the types of questions asked over Memory Allocation in C language, whether it may be a interview questions or something related to your studies.
Questions on Memory Allocation-
Q1. WHAT IS MEMORY ALLOCATION IN C?
ANS. The space of memory block allocated to every variable and piece of code is known as memory allocation in c.
Q2. HOW MANY TYPES OF MEMORY ALLOCATION IS DONE IN C?
ANS. C support two types of memory allocation:
A. Static allocation: when we declare static, global variable in program then a block of code of fixed size is assigned to them and during the program execution it is never freed.
B. Automatic allocation: when functions argument, local variables are declared and then space of an automatic variable block is allocated and after the execution of these statements it is freed.
Q3. WHAT IS STATIC MEMORY ALLOCATION?
ANS. It is the process of allocating memory at compile time before the program code is executed. Static memory allocation may be used to create queues or stacks.
Q4. WHAT IS AUTOMATIC MEMORY ALLOCATION?
ANS. When we declare an automatic variable, function argument, local variable, the space of memory is allocated for the automatic variable. When the statement containing the declaration is executed, then only it is freed.
Q5. WHAT IS DYNAMIC MEMORY ALLOCATION?
ANS. When we don’t know how much amount of memory is needed to store the information of the running program then dynamic allocation of memory is done. When the user explicitly enters a variable according to him then this is done. For eg. in a text decoder it is done.
Q6. WHAT IS THE DIFFERENCE BETWEEN STATIC AND DYNAMIC MEMORY ALLOCATION?
ANS. The Static and Dynamic Memory Allocation can be differentiated as follows-
STATIC MEMORY ALLOCATION
1. Memory is allocated before the execution of the program begins(During Compilation).
2. No memory allocation or deallocation actions are performed during Execution.
3. Variables remain permanently allocated.
4. Implemented using stacks and heaps.
5. Pointer is needed for accessing variables.
6. Faster execution than Dynamic.
7. More memory Space required.
DYNAMIC MEMORY ALLOCATION
1. Memory is allocated during the execution of the program.
2. Memory Bindings are established and destroyed during the Execution.
3. Allocated only when program unit is active.
4. Implemented using data segments.
5. No need of Dynamically allocated pointers.
6. Slower execution than static.
7. Less Memory space required.
Q7. NAME THE FUNCTIONS USED IN C FOR MEMORY MANAGEMENT?
ANS. malloc() , calloc(), realloc(),free() are the functions used in C for memory management.
Q8. WHAT ARE THE COMMON ERRORS IN MEMORY MANAGEMENT?
ANS. Following are the common errors in memory management-
1. Not checking for allocation failures. Memory allocation is not guaranteed to succeed. If there’s no check for successful allocation implemented, this leads to crash of programs
2. Memory leaks. Failure to deallocate memory using free leads to buildup of memory that is non-reusable memory, which is no longer used by the program. This wastes memory resources and can lead to allocation failures when these resources are exhausted.
3. Logical errors.
Q9. WHAT IS FREEING MEMORY?
ANS. Memory allocated with malloc() is when not used then it is freed by the help of function free()
Q10. WHAT IS MEMORY REALLOCATION?
ANS. In this when we allocate a space of memory to a code and then this block of memory is not sufficient for the code then we reallocate the block of memory to the code this is known as memory reallocation.It is done by the help of realloc() function.
Learn more, get the Memory Allocation in C language Questions with their answers in pdf here QUESTIONS ON MEMORY ALLOCATION in Pdf
Would you like to share your knowledge about the C Language, share it in the comment box below the article. Your work will be appreciated.