Deepak Ukey
Deepak Ukey

Reputation: 11

Memory allocation for Inline and normal function

Which memory region is used by Function and function parameter? Also in which region memory for inline function get allocated?
If i am calling the inline function inside the normal function multiple times will memory allocated for the inline function multiple time?

Below is sample program

inline int add (int a, int b)
{
      return A+B;
}

int calculation(int c , int d)
{
    int ret;
    for (int i=0; i < 3; i++) {
           ret = add(c, d);
           c++;
           d++;
     }
     return ret;
}

Where the memory for a& b and c&d will be allocated?

Upvotes: 1

Views: 654

Answers (2)

Lundin
Lundin

Reputation: 213950

Memory regions aren't standardized, though de facto standards like ELF exist, which is a common format both for Unix-like systems and embedded systems.

Assuming an ELF-like system, the region where executable code is stored is called .text. It doesn't matter if a function is inlined or not, it's machine code will end up in that segment.

A normal function stores its parameters either in registers or on the stack. This is system-specific and depends on the "ABI" (Application Binary Interface). When such a function gets inlined, it may not be necessary to copy the variables from the caller, in which case they remain in whatever register or region they were already allocated in.


As for what will happen in your specific code example, the function doesn't contain any side effects and results aren't stored, so only the last lap in the for loop is actually relevant. The loop would have been executed 4 times, so the the various ++ operations just boil down to 2+2=4.

The generated machine code on an optimizing x86 compiler boils down to

lea     eax, [rdi+4+rsi]
ret

Which in the equivalent C code pretty much means that your code was replaced with this:

int calculation(int c , int d)
{
  return c + d + 4;
}

This is because the algorithm itself is nonsense, more so than the inlining. The compiler is perfectly able to inline this without the inline keyboard and will do so with optimizations enabled.

Upvotes: 2

MaanooAk
MaanooAk

Reputation: 2468

Inlining is compiler specific. However you can thing about it like a series of transformations to the code.

Starting from the original code:

ret = add(c, d);

First the function arguments are exported:

int a = c;
int b = d;
ret = add(a, b)

Then the body of the function is inlined:

int a = c;
int b = d;
ret = a + b

Then all kinds of other optimizations will take place, however in the worst case the (just the above code without any optimization), the variables a and b will be in the stack after the ret variable.

The main point is that there will not be many allocations, just one. The int a =... and int b = ... may seem that they are allocated at every loop, but in reality there are allocated with the call to the function, like if they were just after the int ret statement.

Upvotes: 0

Related Questions