A Little about the blog.....

This blog contains all the interview questions that i face during my interviews with various MNC's. I am trying to post the solutions for almost all the questions and i have covered few important topics which would be frequent topics in interviews. I hope the blog would be useful not only for the job aspirants but also to the one who tries to upgrade his/her knowledge in C and Embedded concepts......

please enter your mail address in the above mail address box to get the immediate updates of the new interview questions.....
Showing posts with label stack. Show all posts
Showing posts with label stack. Show all posts

Wednesday, November 16, 2011

Difference between Static Memory Allocation and Dynamic Memory Allocation

In C language, there are two types of memory allocations to any variable.

generally in normal programming, we define and declare the variable with the data type and storage class of the variable and the memory is allocated to the variable. This is called static memory allocation.

But in certain cases where in we don't know about how many variables we are going to use and how much of memory is to be allocated, we opt for at the instant memory allocation during the execution of the program which is called as Dynamic Memory allocation.

for dynamically allocating memory, we make use of
malloc,calloc and realloc functions.

coming to the memory, In static memory allocation (SMA),the memory is allocated in STACK of the RAM and in Dynamic Memory Allocation, the memory is allocated in HEAP of the RAM.

what do u mean by a volatile in C langauge.

The keyword "VOLATILE" is used to define a variable so as to inform the compiler that the variable value can change at any instant of time and is not a constant through out the program.

Interview Question: How the compiler treats any volatile variable?
Explanation: A volatile variable is one whose VALUE CAN CHANGE UNEXPECTEDLY. Consequently, the compiler can make NO ASSUMPTIONS about the value of the variable. In particular,the optimizer must be careful to RELOAD the variable every time it is used instead of holding a copy in a register.

Examples of volatile variables are:

(a) Hardware registers in peripherals (e.g., status registers)
(b) Non-stack variables referenced within an interrupt service routine.
(c) Variables shared by multiple tasks in a multi-threaded application.