C语言 C中指针和全局变量的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3113668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:44:07  来源:igfitidea点击:

Location of pointers and global variables in C

cmemory-management

提问by Muhammad Hewedy

Where are pointersand global variablesstored in C? Are they saved in the memory, heap or stack?

哪里指针全局变量保存在C ^?它们是保存在内存、堆还是堆栈中?

回答by Carl Norum

Global variables can be in a couple places, depending on how they're set up - for example, constglobals may be in a read-only section of the executable. "Normal" globals are in a read-write section of the executable. They're not on the heap or the stack at all. Pointers are just a type of variable, so they can be wherever you want them to be (on the heap if you malloc()them, on the stack if they're local variables, or in the data section if they're global).

全局变量可以位于几个位置,具体取决于它们的设置方式 - 例如,const全局变量可能位于可执行文件的只读部分。“正常”全局变量位于可执行文件的读写部分中。它们根本不在堆或堆栈上。指针只是一种变量,所以它们可以在你想要的任何地方(如果你是在堆上malloc(),如果它们是局部变量,则在堆栈中,或者如果它们是全局变量,则在数据部分)。

回答by Tim Schaeffer

Compile

编译

When a file is compiled, variables with "program duration" (static or global variables) which are defined within that "compilation unit" (in rough terms, the "c file") determine how much global space this program needs, and how it is initialized.

编译文件时,在该“编译单元”(粗略地说,“c 文件”)中定义的具有“程序持续时间”的变量(静态或全局变量)决定了该程序需要多少全局空间,以及它如何被初始化。

Link

关联

When the linker creates your executable, it combines this information and puts it into sections of the executable used for that purpose. The linker then goes through and changes all references to that data to where it will put that data in the process's memory space when the program is loaded. So the pointersto the global data will be like constants in your source file; no memory needs to be set aside for them.

当链接器创建您的可执行文件时,它会组合这些信息并将其放入用于该目的的可执行文件的部分中。然后,链接器遍历并将对该数据的所有引用更改为在加载程序时将该数据放置在进程内存空间中的位置。因此,指向全局数据的指针将类似于源文件中的常量;不需要为它们留出内存。

For zero-initialized or uninitialized global data, just the amount of space needed for such data is stored. For initialized data, the initial values are stored.

对于零初始化或未初始化的全局数据,仅存储此类数据所需的空间量。对于初始化数据,存储初始值。

Load

加载

When your program is loaded, the loader will look into the program file to tell how much zero-initialized data is needs, and sets aside enough of the process's memory space for it, and initializes it all to binary zero. For the initialized data, it sets aside memory for that and initializes it to the initial values saved in the exe file. It also sets aside areas for the heap (used by malloc()) and the stack.

当您的程序被加载时,加载器将查看程序文件以告知需要多少零初始化数据,并为其留出足够的进程内存空间,并将其全部初始化为二进制零。对于初始化的数据,它会为此预留内存并将其初始化为保存在 exe 文件中的初始值。它还为堆(由 malloc() 使用)和堆栈留出区域。

Dynamic Libraries

动态库

If your code is in a dynamic library, the linker can't know where it will put the global data. In this case, it creates sections in the library to tell it where the references to the global data are, and the loader takes care of changing the references to point to the right place when it loadsthe file (this is why you pass -fPIC on the gcc command line for dlls). But since this is done at load time, by the time your program runs the placement of the data is known, so the loader knows where that data will be pointers to global data can still act as constants in your program.

如果您的代码位于动态库中,则链接器无法知道将全局数据放在哪里。在这种情况下,在库创建部分告诉它到全局数据的引用,并加载需要切换到合适的地方点上引用的照顾,当它加载的文件(这就是为什么你传递-fPIC在 dll 的 gcc 命令行上)。但是因为这是在加载时完成的,所以当你的程序运行时,数据的位置是已知的,所以加载器知道数据在哪里,指向全局数据的指针仍然可以作为程序中的常量。

Dynamic Loading

动态加载

If you link dynamically to a dll, the loader doesn't know about it, so you have to call functions which know how to load a dll and get the address of its exported data and functions.

如果您动态链接到 dll,加载程序不知道它,因此您必须调用知道如何加载 dll 并获取其导出数据和函数地址的函数。

回答by sepp2k

Global variables are usually stored in the application's data segment.

全局变量通常存储在应用程序的数据段中。

Pointers aren't stored any differently than other variables (e.g. if you have a local variable of type int*, it will be stored on the stack, the same as any other local variable).

指针的存储方式与其他变量没有任何不同(例如,如果您有一个类型为 的局部变量int*,它将存储在堆栈中,与任何其他局部变量相同)。

回答by Jignesh Patel

All allocation made by malloc(), calloc() or realloc() are stored on the heap, while all local variables are stored on the stack.

malloc()、calloc() 或 realloc() 所做的所有分配都存储在堆中,而所有局部变量都存储在堆栈中。

All global and static variables are stored in the data segment, while constants are stored in the code segment.

所有全局和静态变量都存储在数据段中,而常量存储在代码段中。

回答by Ciaran Archer

Memory via malloc()is taken from the heap. This provides a pointer to the memory.

内存通过malloc()从堆中取出。这提供了一个指向内存的指针。

More info here.

更多信息在这里