Texas Instruments

  www.ti.com
  www.ti.com

Interview Question

Software Engineer Interview(Student Candidate) Tiruchchirāppalli (India)

Driver question and Questions related to how C program is

  stored in memory and why cant we access local variables from other blocks(relating to how they are stored in memory)
Answer

Interview Answer

1 Answer

0

In C, you have 3 "types" of memory: the stack, the heap and the data segment. All the variables declared and used in a c function are allocated on the stack. The most important thing about the stack is that, it is "freed" after execution of the function, i.e. the variables are no longer stored on the stack.

malloc, calloc and realloc calls allow memory allocation on the heap. These memory segments must be managed manually by the programmer, i.e. the programmer is responsible to free the heap. So it is critical to always have a pointer pointed to the allocated segment, in order to free it later. Variables on the heap can be accessed from outside the functions they are declared in. A "memory leak" occurs, when a variable allocated on the heap does not get freed when the execution of the whole code is done.

All global variables, static variables and function return values are stored in the data segment.

This is how I would answer such a question. These are very basic stuff, that every firmware developer must know!

Hachem on Nov 19, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.