Imagine I have the following code
void A(){
// blah blah blah
}
void B(){
// blah blah blah
}
void C(){
// blah blah blah
}
When the code’s compiled (using Visual Studio) and loaded into memory to execute, the addresses of A(), B(), C() may not in sequential order. How can we make them ordered? Is there any directive useful in this situation?
If your program consists of a single translation unit (source file), the resulting binary should usually contain the functions in the order in which they are defined in the translation unit. For example, consider the following program:
If you compile this program optimized for minimum size (/O1) and with inline function expansion disabled (/Ob0), the following machine code is produced:
The source annotations are provided by the debugger. Optimization for minimum size is required to eliminate the
int 3padding that is placed between functions when other optimization settings are used. Inline function expansion must be disabled to ensure that the functions actually exist in the resulting binary.This is, to the best of my knowledge, an implementation detail, so it should not be relied upon in production code.