开发者

Function allocation

开发者 https://www.devze.com 2022-12-27 05:00 出处:网络
Where are functions stored in a C++ program? For example in开发者_运维知识库t abc() { //where am I stored?

Where are functions stored in a C++ program?

For example

in开发者_运维知识库t abc()
{
   //where am I stored? 
}

I know that we can take the address of a function, that means functions are stored somewhere in memory. But I have already read at many places that no memory allocation for functions takes place.

I am confused. My question may seem vague to many of you but I can't help.


Before being run, your program is loaded into memory, and that includes loading the code that implements the functions.

Once the program starts to run, no memory allocation for functions takes place; it's done before the program starts, by the system's program loader.

This assumes a "normal" desktop OS, for embedded systems running code out of ROM, the situation is often different.


The location and order of the functions in memory is controlled by linker and can be adjusted by editing the linker command file.

This is important especially in the embedded systems. For example, you may want to specify which functions are stored in the fast internal memory and which ones in slower external memory. The order of the functions is important for optimizing the cache, etc.

To find out where each function and (global) variable is stored, look for *.map file created by the linker.


To expand on the other two answers:

On most (non-embedded) platforms, when a unit is compiled, the code and data are stored in sections of an object file. The linker combines these sections together when it assembles the final program. In COFF, PE, and ELF object formats, for example, all code is placed in a .text section. All preinitialized data is stored in the .data or .bss section.

Where these segments actually are is really not important. When the program is loaded, the runtime linker (called ld-linux.so on Linux) will load the entire program to one or more regions of memory and the operating system will map each of the sections to their own memory segments. This assumes the platform has both an MMU and memory segmentation, as on x86. Many modern operating systems also randomize the locations where these sections are loaded, for security. So for each run of the program, a given function may not have the same address.


In some operating systems, functions can be stored on the disk until they are accessed. The OS can reserve an area for loading these functions on demand. For more information search for "Operating System Paging".

Some compilers and linkers allow the programmer to specify function locations. For convenience they define segments and allow the segments to be placed at different locations. On embedded systems, this allows for some functions to reside in ROM, some in Flash and others in RAM.

In most instances, programs don't really care where functions are in memory. The compiler generates "Position Independent Code" and the operating system assigns physical addresses when the program is loaded.

0

精彩评论

暂无评论...
验证码 换一张
取 消