INTERVIEW QUESTIONS
C
DYNAMIC MEMORY ALLOCATION IN C
DETAILS
Question: What is static memory allocation and dynamic memory allocation?
Answer: Static memory allocation: The compiler allocates the required memory space for a declared variable.By using the address of operator,the reserved address is obtained and this address may be assigned to a pointer variable.Since most of the declared variable have static memory,this way of assigning pointer value to a pointer variable is known as static memory allocation. memory is assigned during compilation time.<br><br>Dynamic memory allocation: It 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 functions are assingned to pointer variables, such assignments are known as dynamic memory allocation.memory is assined during run time. <br>
|
Question:
What is static memory allocation and dynamic memory allocation?
Answer:
Static memory allocation: The compiler allocates the required memory space for a declared variable.By using the address of operator,the reserved address is obtained and this address may be assigned to a pointer variable.Since most of the declared variable have static memory,this way of assigning pointer value to a pointer variable is known as static memory allocation. memory is assigned during compilation time.<br><br>Dynamic memory allocation: It 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 functions are assingned to pointer variables, such assignments are known as dynamic memory allocation.memory is assined during run time. <br> Source: CoolInterview.com
Static memory allocation will be stored in Stack or in Data Segment.<br><br>say,<br><br>int i;<br>char ch;<br><br>int array[5];<br>float f;<br><br>struct xyz obj1;<br>etc...<br><br>Global variables are stored in Data Segment which is also static memory allocation.<br><br>int index;<br>double salary;<br><br>int main()<br>{<br>;;; // Some thing<br>}<br><br><br>Where as the dynamic memory allocation is done in heap using malloc(),calloc() or even realloc(). Source: CoolInterview.com
Answered by: Syed Baseer Ahmed | Date: 12/5/2007
| Contact Syed Baseer Ahmed
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|