6

I have a function of this form:

void authenticate()
{
    int auth_flag;
    char password[16];
    ...
}

When I debug the program I can see that the auth_flag variable is after the password variable in the stack (which seems normal).

Now when I change the order of variable declarations:

void authenticate()
{
    char password[16];
    int auth_flag;
    ...
}

I see that variable auth_flag is still allocated after the password variable in the stack.

What I'm looking for is any way to avoid/control that, whether with a compilation option or in-code compiler directives.

B. Kostas
  • 583
  • 4
  • 17
  • 2
    Why do you want to prevent it? If it is so necessary you might want to declare an anonymous structure. – zapstar Oct 17 '16 at 07:45
  • I want to avoid the special treatment of structs by the gcc. What I'm looking is more like a gcc option to achieve the results. – B. Kostas Oct 17 '16 at 07:48
  • @KenY-N That post explains the reason but I still want to know if there's an option to control that gcc functionality. – B. Kostas Oct 17 '16 at 07:50
  • 1
    [How does GCC decide what order to output assembly functions in?](http://stackoverflow.com/q/6886567/995714), [How can I force the order of functions in a binary with the gcc toolchain?](http://stackoverflow.com/q/6614209/995714) – phuclv Oct 17 '16 at 08:38
  • 1
    @Lưu Vĩnh Phúc : ordering of variables was asked, not functions. The links you provided do not propose any solution for that. – Pyves Oct 17 '16 at 08:46

2 Answers2

4

According to GCC documentation "Common Function Attributes":

  • no_reorder

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.

And in "Optimize Options":

  • -fno-toplevel-reorder

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.

Community
  • 1
  • 1
  • This is an answer to a different question. This question is asking about order of locals (automatic storage) inside the stack-frame of a single function. You're answering about the *code* (and static storage) for multiple things at global scope. IDK why the OP accepted this! – Peter Cordes Oct 31 '19 at 04:55
1

In truth you program the compiler the compiler then programs the machine. The compiler will be making decision regarding your code. It will do things on the stack that may help with caching and thus may move things around. There will be compiler options to stop it doing this. Indeed you may need the volatile keyword to stop it removing variables altogether.

cdcdcd
  • 547
  • 1
  • 5
  • 15
  • 1
    `volatile` does not override / control allocation order of locals, only reordering of *accesses*. If you want to control where things are relative to each other, put them in a `struct`. – Peter Cordes Oct 31 '19 at 04:56