i'm currently in cprogramming.com's c++ tutorial section and in the inline functions lesson...

i read that when an inline function is called the compiler replaces the function call with the actual code...

if that is how an inline function is called, then how is a normal function called?

10-27-2012

GReaper

Briefly, the next instruction's address is pushed on the stack, then the passed arguments are pushed onto the stack and finally the execution jumps to where the function code is. When finished, execution jumps right after the call, thanks to that previously pushed address. :p

EDIT: Or is the address pushed last? I don't remember...

10-27-2012

grumpy

Quote:

Originally Posted by tennisstar

i read that when an inline function is called the compiler replaces the function call with the actual code...

That is a vast oversimplification.

When inlining a function call, the compiler still needs to take steps to preserve scope. For example, if a function changes an argument that has been passed by value, that change should not propagate to the caller.

The starting point for inlining a function is - conceptually at least - the means of calling it without inlining (because the semantics of the function should not change by inlining it). The difference is that, since the compiler has visibility of code in both the caller and callee, it can eliminate unnecessary steps (eg pushing values to a stack before, popping them off later, clearing and resetting registers, shadowing of variables, etc).

Note also that a programmer declaring a function inline (or defining it inline, say within a class definition) is only a hint to the compiler. A compiler may ignore that hint. Modern compilers often do ignore that hint because they can do a better job of spotting and exploiting opportunities for inlining than a typical programmer can - and because, depending on how the compiler works, not all functions can be inlined anyway. Similarly, modern compilers (or even linkers) can elect to inline a function, even if they haven't been asked to.

Quote:

Originally Posted by GReaper

Briefly, the next instruction's address is pushed on the stack, then the passed arguments are pushed onto the stack and finally the execution jumps to where the function code is. When finished, execution jumps right after the call, thanks to that previously pushed address. :p

EDIT: Or is the address pushed last? I don't remember...

What you've described (or attempted to describe) is specific to one compiler. It is not general to all compilers.

The details of how inlining is achieved (or not, as the case may be) are part of the "secret sauce" of most compilers.

10-27-2012

Elysia

Quote:

Originally Posted by GReaper

Briefly, the next instruction's address is pushed on the stack, then the passed arguments are pushed onto the stack and finally the execution jumps to where the function code is. When finished, execution jumps right after the call, thanks to that previously pushed address. :p

EDIT: Or is the address pushed last? I don't remember...

In x86 assembly, the call function pushes the address to the stack before jumping, so it would be pushed last.
Just to point out to the curious. This is, after all, compiler and platform specific.