Where in memory are my variables stored in C?
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:
char *arr
, int *arr
) -------> heap I am referring to these variables only from the C perspective.
Please correct me if I am wrong as I am new to C.
You got some of these right, but whoever wrote the questions tricked you on at least one question:
main
function -----> heap also stack (the teacher was trying to trick you) char *arr
, int *arr
) -------> heap data or stack, depending on the context. C lets you declare a global or a static
pointer, in which case the pointer itself would end up in the data segment. malloc
, calloc
, realloc
) --------> stack heap It is worth mentioning that "stack" is officially called "automatic storage class".
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 using size
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 variable static int localStatic;
will be stored in the uninitialized data segment. 0
or NULL
then still it would go to uninitialized data segment or bss. 3. Initialized data segment
int globalVar = 1;
or static local variable static int localStatic = 1;
will be stored in initialized data segment. 4. Stack Segment
5. Heap Segment
malloc
, calloc
, or realloc
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 in ptr
variable. The ptr
variable will be on either the stack or data segment depending on the way it is declared/used. Corrected your wrong sentences
constant data types -----> code //wrong
local constant variables -----> stack
initialized global constant variable -----> data segment
uninitialized global constant variable -----> bss
variables declared and defined in main function -----> heap //wrong
variables declared and defined in main function -----> stack
pointers(ex:char *arr,int *arr) -------> heap //wrong
dynamically allocated space(using malloc,calloc) --------> stack //wrong
pointers(ex:char *arr,int *arr) -------> size of that pointer variable will be in stack.
Consider that you are allocating memory of n bytes (using malloc
or calloc
) dynamically and then making pointer variable to point it. Now that n
bytes of memory are in heap and the pointer variable requries 4 bytes (if 64 bit machine 8 bytes) which will be in stack to store the starting pointer of the n
bytes of memory chunk.
Note : Pointer variables can point the memory of any segment.
int x = 10;
void func()
{
int a = 0;
int *p = &a: //Now its pointing the memory of stack
int *p2 = &x; //Now its pointing the memory of data segment
chat *name = "ashok" //Now its pointing the constant string literal
//which is actually present in text segment.
char *name2 = malloc(10); //Now its pointing memory in heap
...
}
dynamically allocated space(using malloc,calloc) --------> heap
链接地址: http://www.djcxy.com/p/28342.html上一篇: 堆栈溢出在Linux上沉默?
下一篇: 内存中的位置是我的变量存储在C中?