tags:

views:

110

answers:

4

Where are functions stored in a C++ program?

For example

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

+9  A: 

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.

unwind
Also: it may be loaded dynamically by virtual memory.
Potatoswatter
+2  A: 

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.

PauliL
A: 

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.

greyfade
Segmented memory management is rarely used these days. Even Windows now uses linear addressing. Old .COM programs in DOS relied on segments, but .EXE programs contain relocating information and therefore do not need segmentation hardware.
PauliL
@PauliL: While that's true, the concept is still around. The program sections are still clearly delineated and and may also exist in distinct pages that are used much in the same way that the segment registers once were.
greyfade
A: 

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.

Thomas Matthews
Position independent code and relocatable code are two different things. When relocatable code is loaded, the loader calculates the absolute addresses and updates those addresses in the code. Position independent code does not need relocating. The addresses are relative (e.g. jump 215 bytes backward from current location), so the code works without modification at any memory location.
PauliL