How will i know whether inline function is actually replaced at the place where it is called or not?
Solution 1
Programatically at run-time, You cannot.
And the truth of the matter is: You don't need to know
An compiler can choose to inline
functions that are not marked inline
or ignore functions marked explicitly inline
, it is completely the wish(read wisdom) of the compiler & You should trust the compiler do its job judiciously. Most of the mainstream compilers will do their job nicely.
If your question is purely from a academic point of view then there are a couple of options available:
Analyze generated Assembly Code:
You can check the assembly code to check if the function code is inlined at point of calling.
How to generate the assembly code?
For gcc:
Use the -S
switch while compilation.
For ex:
g++ -S FileName.cpp
The generated assembly code is created as file FileName.s
.
For MSVC:
Use the /FA Switch from command line.
In the generated assembly code lookup if there is a call
assembly instruction for the particular function.
Use Compiler specific Warnings and Diagnostics:
Some compilers will emit a warning if they fail to comply an inline function request.
For example, in gcc, the -Winline
command option will emit a warning if the compiler does not inline a function that was declared inline.
Check the GCC documentation for more detail:
-Winline
Warn if a function that is declared as inline cannot be inlined. Even with this option, the compiler does not warn about failures to inline functions declared in system headers.
The compiler uses a variety of heuristics to determine whether or not to inline a function. For example, the compiler takes into account the size of the function being inlined and the amount of inlining that has already been done in the current function. Therefore, seemingly insignificant changes in the source program can cause the warnings produced by
-Winline
to appear or disappear.
Solution 2
Check the generated code. If the function is expanded, you'll see its body, as opposed to a call
or similar instruction.
Solution 3
You can use tools for listing symbols from object files such as nm
on Linux. If the function was inlined, it will not be listed in nm
output - it became part of some other function. Also you will not be able to put breakpoint on this function by name in debugger.
Solution 4
If you need to make sure that function is inlined and OK to go with proprietary extension in MS VC++, check out the __forceinline
declarator. The compiler will either inline the function or, if it falls into the list of documented special cases, you will get a warning - so you will know the inlining status.
Not endorsing it in any way.
Solution 5
With gdb, if you cannot call to a function, one of its possible meanings is the function is inline. Flipping the reasoning, if you can call a function inside gdb, means the function is not marked inline.
Related videos on Youtube
Comments
-
Abhineet about 2 years
I know that inline function are either replaced where it is called or behave as a normal function.
But how will I know whether inline function is actually replaced at the place where it is called or not as decision of treating inline function as inline is at the compile time?
-
Jonathan Leffler about 12 yearsYou won't; it has to behave the same regardless.
-
Peter Wood about 12 yearsSee Herb Sutter's Inline Redux
-
-
Abhineet about 12 yearsYou mean while debugging, in assembly code section, i need to check whether the inline function is behaving as an inline or not. Something like if at the assembly side if this function is pushed and popped out etc...then it will be behaving as a normal function else inline..
-
Alok Save about 12 years@Abhineet: Yes, or you can use compiler specific flags which warn of not being ably to complying to
inline
requests. -
Alok Save about 12 yearsThe same function might be inlined at one calling instance and may not be inlined at another, it entirely depends on the compiler.So using
nm
is not a reliable way of determining if the function call was indeed inlined. -
ks1322 about 12 years@Als: Ok, if the function is absent in
nm
output, this means that all it's instances were inlined. Still it gives some information about inlining. -
Alok Save about 12 yearsYes,it gives some information, I wanted to make it clear that using
nm
tells you if all calling instances of a function in an translation unit wereinline
d or not, it doesn't give the information whether a particular calling instance wasinline
d. -
antiHUMAN about 8 yearsYes,
__forceinline
removes the compiler's cost/benefit calculation, and inlines the function if it is possible. It's important to note that it only disables the cost/benefit calculation, but doesn't 100% guarantee that it gets inlined. -
AlwaysLearning over 7 yearsAlso, if inlining is supposed to affect run-time, then compare the run-time with and without
__attribute__ ((noinline))
in the function definition right after the return type (note that this isgcc
-specific). -
fider about 7 years+1 for
-Winline
. Here you have the non-academic reason to check it and to force inlinig. (..networking/serialization library where it wont inline functions by default because of code size..) -
Alexander over 6 years
You don't need to know
Not necessarily. If you want a helper function that wraps assembly code, it very much matters. -
ilw over 6 yearsin
gcc, the -Winline
- how do thaincl
? -
Przemek about 6 yearsIn default mode,
gcc
doesn't inline functions at all and-Winline
doesn't inform about that fact.gcc file.c -Winline
will compile without warnings, and all inline specifiers will be ignored.-Winline
works properly only when optimisation flag is enabled (-O1
,-O2
, etc.). -
Alexey about 5 yearsYou aren't right about "You don't need to know". In my code I use stack overflow guards, so in case when a function gets inlined the checking code is an excessive overhead. So I want to know...
-
Nanashi No Gombe about 4 yearsWhat is an example of a "similar instruction" to
call
? Sorry, I do not know much about assembly. -
Andrew over 3 years
You don't need to know
Once more I will add, BS. Don't tell us what we do and don't need to know, you'll likely frequently be wrong. In my case, it's for crucially fast code, and I do need to know what it's going to do.