Search code examples
cstructureinline

Inline function, pointer to variable


I am trying to understand how the inline keyword works with pointers to variables.

Consider the following example:

struct S
{
 float a;
 float b;
};

inline void foo (struct S *s)
{
   s->a = 5;
}

void main()
{
 struct S ss;
 foo(&ss);
}

When the compiler inlines the function foo, will it generate

void main()
{
 struct S ss;
 (&ss)->a = 5;
}

Or will it generate

void main()
{
 struct S ss;
 ss.a = 5;
}

In other words, will the code need to de-reference the pointer to the structure or will it understand that it needs to replace by just the structure?

In an embedded application, this would make a difference in runtime that could be significant.


Solution

  • The compiler is required only to generate code that achieves the result required by the semantics of the language. How a specific compiler achieves that is entirely implementation dependent. It is even possible that the code will not be in-lined at all.

    To determine how your particular compiler will translate this code, you can either instruct it to output an assembly listing of the generated code or inspect the code disassembly in a debugger. The code generated may also be very different depending upon compiler options such as optimisation level.