Where in memory are my variables stored in C?
Solution 1:
You got some of these right, but whoever wrote the questions tricked you on at least one question:
- global variables -------> data (correct)
- static variables -------> data (correct)
- constant data types -----> code and/or data. Consider string literals for a situation when a constant itself would be stored in the data segment, and references to it would be embedded in the code
- local variables(declared and defined in functions) --------> stack (correct)
- variables declared and defined in
main
function ----->heapalso stack (the teacher was trying to trick you) - pointers(ex:
char *arr
,int *arr
) ------->heapdata or stack, depending on the context. C lets you declare a global or astatic
pointer, in which case the pointer itself would end up in the data segment. - dynamically allocated space(using
malloc
,calloc
,realloc
) -------->stackheap
It is worth mentioning that "stack" is officially called "automatic storage class".
Solution 2:
For those future visitors who may be interested in knowing about those memory segments, I am writing important points about 5 memory segments in C:
Some heads up:
- Whenever a C program is executed some memory is allocated in the RAM for the program execution. This memory is used for storing the frequently executed code (binary data), program variables, etc. The below memory segments talks about the same:
- Typically there are three types of variables:
- Local variables (also called as automatic variables in C)
- Global variables
- Static variables
- You can have global static or local static variables, but the above three are the parent types.
5 Memory Segments in C:
1. Code Segment
- The code segment, also referred as the text segment, is the area of memory which contains the frequently executed code.
- The code segment is often read-only to avoid risk of getting overridden by programming bugs like buffer-overflow, etc.
- The code segment does not contain program variables like local variable (also called as automatic variables in C), global variables, etc.
- Based on the C implementation, the code segment can also contain read-only string literals. For example, when you do
printf("Hello, world")
then string "Hello, world" gets created in the code/text segment. You can verify this usingsize
command in Linux OS. - Further reading
Data Segment
The data segment is divided in the below two parts and typically lies below the heap area or in some implementations above the stack, but the data segment never lies between the heap and stack area.
2. Uninitialized data segment
- This segment is also known as bss.
- This is the portion of memory which contains:
- Uninitialized global variables (including pointer variables)
- Uninitialized constant global variables.
- Uninitialized local static variables.
- Any global or static local variable which is not initialized will be stored in the uninitialized data segment
- For example: global variable
int globalVar;
or static local variablestatic int localStatic;
will be stored in the uninitialized data segment. - If you declare a global variable and initialize it as
0
orNULL
then still it would go to uninitialized data segment or bss. - Further reading
3. Initialized data segment
- This segment stores:
- Initialized global variables (including pointer variables)
- Initialized constant global variables.
- Initialized local static variables.
- For example: global variable
int globalVar = 1;
or static local variablestatic int localStatic = 1;
will be stored in initialized data segment. - This segment can be further classified into initialized read-only area and initialized read-write area. Initialized constant global variables will go in the initialized read-only area while variables whose values can be modified at runtime will go in the initialized read-write area.
- The size of this segment is determined by the size of the values in the program's source code, and does not change at run time.
- Further reading
4. Stack Segment
- Stack segment is used to store variables which are created inside functions (function could be main function or user-defined function), variable like
- Local variables of the function (including pointer variables)
- Arguments passed to function
- Return address
- Variables stored in the stack will be removed as soon as the function execution finishes.
- Further reading
5. Heap Segment
- This segment is to support dynamic memory allocation. If the programmer wants to allocate some memory dynamically then in C it is done using the
malloc
,calloc
, orrealloc
methods. - For example, when
int* prt = malloc(sizeof(int) * 2)
then eight bytes will be allocated in heap and memory address of that location will be returned and stored inptr
variable. Theptr
variable will be on either the stack or data segment depending on the way it is declared/used. - Further reading