我有这种形式的功能:
void authenticate()
{
int auth_flag;
char password[16];
...
}
当我调试程序时,我可以看到 auth_flag 变量在堆栈中的 password 变量之后(这看起来很正常)。
现在当我改变变量声明的顺序时:
void authenticate()
{
char password[16];
int auth_flag;
...
}
我看到变量 auth_flag 仍然分配在堆栈中的 password 变量之后。
我正在寻找的是避免/控制这种情况的任何方法,无论是使用编译选项还是代码内编译器指令。
根据 GCC documentation "Common Function Attributes" :
Do not reorder functions or variables marked no_reorder
against each other or top level assembler statements the executable. The actual order in the program will depend on the linker command line. Static variables marked like this are also not removed. This has a similar effect as the -fno-toplevel-reorder
option, but only applies to the marked symbols.
并且在 "Optimize Options" :
Do not reorder top-level functions, variables, and asm statements. Output them in the same order that they appear in the input file. When this option is used, unreferenced static variables are not removed. This option is intended to support existing code that relies on a particular ordering. For new code, it is better to use attributes when possible.