0

I'm trying to study the intricacies of inline. To that end I've created this little program. I've defined an inline function that has an 'automatic' variable of the same name as a globally defined variable. After making sure my function is marked static (to satisfy the C99 standard), I'm studying the executable that got created using objdump. But first, here's my program

#include <stdio.h>
#include <stdlib.h>

int AAAABBBB = 5; //global scope

#define DOINLINE$

#ifdef DOINLINE
static inline void  __attribute__((always_inline)) myfunc() {
#else
void myfunc() {
#endif
    static int AAAABBBB = 6;
    printf("global is now %d\n", AAAABBBB);
}

int main() {
    printf("global is %d\n", AAAABBBB);
    myfunc();
    printf("global is now %d\n", AAAABBBB);
    return 0;
}

This function works as expected and there is no conflict whatsoever between those seemingly conflicting names. However, looking at the object dump of the executable (as well as the object file) I see that the compiler has indeed honored my request and there is no separate section for myfunc.

Here's the paste of the objdump. I'm sure the local variable inside myfunc is buried somewhere in it but can't seem to figure out where it is.

Note

Declaring the function local variable as non-static seem to have no bearing at all.

6
  • Why do you think there is a conflict? Local names shadow global names, that's the rules of C++. It's true whether your declare your function inline or not. You seem to think that inline works as some kind of macro, substituting the text of the function into your source code but that's not the case at all.
    – john
    Mar 19, 2020 at 10:59
  • Do you want to know differences between C and C++ or why do you use both tags?
    – Gerhardh
    Mar 19, 2020 at 11:00
  • So.... what is your question?
    – bolov
    Mar 19, 2020 at 11:03
  • Where in the dump is my local variable? Does it differ between c and c++?
    – Skegg
    Mar 19, 2020 at 11:08
  • 1
    @HighOnMeat Presumably it's been optimised away. Neither C or C++ specifies how a compiler should generate object code. As long as the object code does what it should anything is allowed.
    – john
    Mar 19, 2020 at 11:13

1 Answer 1

2

The AAAABBBB defined inside myfunc does not appear in the object file because the compiler optimized it away. We can see this in the disassembly of main, where this is the code that resulted from inlining myfunc:

    1154:   be 06 00 00 00          mov    esi,0x6
    1159:   48 8d 3d b2 0e 00 00    lea    rdi,[rip+0xeb2]        # 2012 
    1160:   b8 00 00 00 00          mov    eax,0x0
    1165:   e8 c6 fe ff ff          call   1030 

Here we see that the compiler merely passed 6 directly to printf; it was never stored in memory. The AAAABBBB in myfunc has no existence in the object file other than this.

One might wonder how a static object, which is supposed to exist for the entire execution of the program, can be removed by the compiler during optimization. The C standard says, in effect, a C implementation may optimize a program in any way that does not change the observable behavior, which is:

  • Accesses to volatile objects are evaluated strictly.
  • When the program ends, data written into files is as defined.
  • Interactions with interactive devices, such as the terminal, are as specified in the “Files” clause.

Since whether data for a static (but not volatile) object is written to memory is not part of the observable behavior (and whether there is actually memory for it at all is not observable behavior), the compiler is free to remove it during optimization.

3
  • So is this the standard GCC behaviour for local inline variables?? Does static have no bearing on it? What if I call it like multiple times?
    – Skegg
    Mar 19, 2020 at 11:27
  • @HighOnMeat: If you call the function multiple times, the only responsibility of the compiler is to produce an object file that has the same observable behavior as your source code. It can do that by passing an immediate value to printf each time, or it can use a memory location for AAAABBBB. It is entirely subject to the optimization capabilities of the compiler. Mar 19, 2020 at 11:52
  • @HighOnMeat: As written, I think AAAABBBB could be optimized away if the function had external linkage instead of internal, since AAAABBBB is never changed—the only effect on observable behavior of any call to it is to print a known string. If the function were changed to include a ++AAAABBBB; and had external linkage, then a compiler would be compelled to instantiate AAAABBBB (unless the build tools can optimize across object modules or translation units). Mar 19, 2020 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.