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

时间:2022-09-12 03:10:07

Where are pointers and global variables stored in C? Are they saved in the memory, heap or stack?

指针和全局变量存储在C中的哪里?它们是保存在内存,堆还是堆栈中?

5 个解决方案

#1


23  

Global variables can be in a couple places, depending on how they're set up - for example, const globals 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()它们在堆上,如果它们是局部变量则在堆栈上,或者如果它们是全局变量则在数据部分中) 。

#2


8  

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 pointers to 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 loads the 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在dcc的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并获取其导出的数据和函数的地址的函数。

#3


6  

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 *类型的局部变量,它将存储在堆栈中,与任何其他局部变量相同)。

#4


4  

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.

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

#5


-2  

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

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

More info here.

更多信息在这里。

#1


23  

Global variables can be in a couple places, depending on how they're set up - for example, const globals 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()它们在堆上,如果它们是局部变量则在堆栈上,或者如果它们是全局变量则在数据部分中) 。

#2


8  

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 pointers to 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 loads the 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在dcc的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并获取其导出的数据和函数的地址的函数。

#3


6  

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 *类型的局部变量,它将存储在堆栈中,与任何其他局部变量相同)。

#4


4  

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.

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

#5


-2  

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

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

More info here.

更多信息在这里。