By considering that the memory is divided into four segments: data, heap, stack, and code, where do global variables, static variables, constant data types, local variables (defined and declared in functions), variables (in main function), pointers, and dynamically allocated space (using malloc and calloc) get stored in memory?
I think they would be allocated as follows:
- Global variables -------> data
- Static variables -------> data
- Constant data types -----> code
- Local variables (declared and defined in functions) --------> stack
- Variables declared and defined in main function -----> heap
- Pointers (for example,
char *arr
,int *arr
) -------> heap - Dynamically allocated space (using malloc and calloc) --------> stack
I am referring to these variables only from the C perspective.
Please correct me if I am wrong as I am new to C.
Nope, they can be on the stack or in the data segment. They can point anywhere.
You got some of these right, but whoever wrote the questions tricked you on at least one question:
main
function ----->heapalso stack (the teacher was trying to trick you)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.malloc
,calloc
,realloc
) -------->stackheapIt is worth mentioning that "stack" is officially called "automatic storage class".
A popular desktop architecture divides a process's virtual memory in several segments:
Text segment: contains the executable code. The instruction pointer takes values in this range.
Data segment: contains global variables (i.e. objects with static linkage). Subdivided in read-only data (such as string constants) and uninitialized data ("BSS").
Stack segment: contains the dynamic memory for the program, i.e. the free store ("heap") and the local stack frames for all the threads. Traditionally the C stack and C heap used to grow into the stack segment from opposite ends, but I believe that practice has been abandoned because it is too unsafe.
A C program typically puts objects with static storage duration into the data segment, dynamically allocated objects on the free store, and automatic objects on the call stack of the thread in which it lives.
On other platforms, such as old x86 real mode or on embedded devices, things can obviously be radically different.
From the perspective of the C language, all that matters is extent, scope, linkage, and access; exactly how items are mapped to different memory segments is up to the individual implementation, and that will vary. The language standard doesn't talk about memory segments at all. Most modern architectures act mostly the same way; block-scope variables and function arguments will be allocated from the stack, file-scope and static variables will be allocated from a data or code segment, dynamic memory will be allocated from a heap, some constant data will be stored in read-only segments, etc.
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:
5 Memory Segments in C:
1. Code Segment
printf("Hello, world")
then string "Hello, world" gets created in the code/text segment. You can verify this usingsize
command in Linux OS.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
int globalVar;
or static local variablestatic int localStatic;
will be stored in the uninitialized data segment.0
orNULL
then still it would go to uninitialized data segment or bss.3. Initialized data segment
int globalVar = 1;
or static local variablestatic int localStatic = 1;
will be stored in initialized data segment.4. Stack Segment
5. Heap Segment
malloc
,calloc
, orrealloc
methods.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.