考虑到内存分为四个部分:数据、堆、堆栈和代码,全局变量、静态变量、常量数据类型、局部变量(在函数中定义和声明)、变量(在主函数中)、指针和动态分配的空间(使用malloc和calloc)存储在内存中?

我认为他们的分配如下:

全局变量-------> data 静态变量------->数据 常量数据类型----->代码 局部变量(在函数中声明和定义)-------->堆栈 主函数-----> heap中声明和定义的变量 指针(例如char *arr, int *arr) ------->堆 动态分配空间(使用malloc和calloc) -------->堆栈

我仅从C的角度引用这些变量。

如果我错了,请指正,因为我是新来C的。


当前回答

有些问题你答对了,但写问题的人至少在一个问题上骗了你:

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 -----> heap also stack (the teacher was trying to trick you) pointers(ex: 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. dynamically allocated space(using malloc, calloc, realloc) --------> stack heap

值得一提的是,“栈”的正式名称是“自动存储类”。

其他回答

我仅从C的角度引用这些变量。

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.

纠正错误的句子

constant data types ----->  code //wrong

本地常量变量----->堆栈

初始化全局常量变量----->数据段

未初始化的全局常量变量-----> BSS

variables declared and defined in main function  ----->  heap //wrong

在主函数-----> stack中声明和定义的变量

pointers(ex:char *arr,int *arr) ------->  heap //wrong

dynamically allocated space(using malloc,calloc) --------> stack //wrong

指针(例如:char *arr,int *arr) ------->该指针变量的大小将在堆栈中。

假设您正在动态分配n字节的内存(使用malloc或calloc),然后使指针变量指向它。现在n个字节的内存在堆中,指针变量需要4个字节(如果64位机器是8个字节),它将在堆栈中存储n个字节的内存块的起始指针。

注意:指针变量可以指向任何段的内存。

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
...
}

动态分配空间(使用malloc,calloc) -------->堆

关于存储需要记住的一件事是假设规则。编译器不需要把一个变量放在特定的位置——相反,它可以把它放在任何它喜欢的地方,只要编译后的程序按照抽象C机器的规则运行,就像它在抽象C机器中运行一样。这适用于所有存储期限。例如:

a variable that is not accessed all can be eliminated completely - it has no storage... anywhere. Example - see how there is 42 in the generated assembly code but no sign of 404. a variable with automatic storage duration that does not have its address taken need not be stored in memory at all. An example would be a loop variable. a variable that is const or effectively const need not be in memory. Example - the compiler can prove that foo is effectively const and inlines its use into the code. bar has external linkage and the compiler cannot prove that it would not be changed outside the current module, hence it is not inlined. an object allocated with malloc need not reside in memory allocated from heap! Example - notice how the code does not have a call to malloc and neither is the value 42 ever stored in memory, it is kept in a register! thus an object that has been allocated by malloc and the reference is lost without deallocating the object with free need not leak memory... the object allocated by malloc need not be within the heap below the program break (sbrk(0)) on Unixen...

流行的桌面架构将进程的虚拟内存划分为几个部分:

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.

C程序通常将具有静态存储持续时间的对象放入数据段中,在空闲存储区中动态分配对象,在其所在线程的调用堆栈中自动分配对象。

在其他平台上,如旧的x86真实模式或嵌入式设备上,情况显然完全不同。

有些问题你答对了,但写问题的人至少在一个问题上骗了你:

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 -----> heap also stack (the teacher was trying to trick you) pointers(ex: 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. dynamically allocated space(using malloc, calloc, realloc) --------> stack heap

值得一提的是,“栈”的正式名称是“自动存储类”。