49

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?

2

10 Answers 10

73

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.

10
  • You 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..
    – Abhineet
    May 17, 2012 at 7:06
  • @Abhineet: Yes, or you can use compiler specific flags which warn of not being ably to complying to inline requests.
    – Alok Save
    May 17, 2012 at 7:08
  • 9
    You don't need to know Not necessarily. If you want a helper function that wraps assembly code, it very much matters.
    – Alexander
    Oct 27, 2017 at 14:24
  • 4
    You 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...
    – Alexey
    May 20, 2019 at 6:43
  • 5
    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.
    – Andrew
    Feb 4, 2021 at 1:16
13

Check the generated code. If the function is expanded, you'll see its body, as opposed to a call or similar instruction.

2
  • 4
    What is an example of a "similar instruction" to call? Sorry, I do not know much about assembly. Jun 22, 2020 at 15:26
  • This answer is better than Alok's which is overly opinionated and wordy.
    – steve
    Apr 3 at 16:53
9

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.

3
  • 5
    The 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.
    – Alok Save
    May 17, 2012 at 8:17
  • 7
    @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.
    – ks1322
    May 17, 2012 at 8:40
  • 1
    Yes,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 were inlined or not, it doesn't give the information whether a particular calling instance was inlined.
    – Alok Save
    May 18, 2012 at 2:55
9

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.

1
  • 4
    Yes, __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.
    – antiHUMAN
    Apr 18, 2016 at 12:51
6

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.

3

The decision to inline or not a function is made by compiler. And since it is made by compiler, so YES, it can be made at compile time only.

So, if you can see the assembly code by using -S option (with gcc -S produces assembly code), you can see whether your function has been inlined or not.

1

There is a way to determine if a function is inline programmatically, without looking at the assembly code. This answer is taken from here.

Say you want to check if a specific call is inlined. You would go about like this. Compiler inlines functions, but for those functions that are exported (and almost all function are exported) it needs to maintain a non-inlined addressable function code that can be called from the outside world.

To check if your function my_function is inlined, you need to compare the my_function function pointer (which is not inlined) to the current value of the PC. Here is how I did it in my environment (GCC 7, x86_64):

void * __attribute__((noinline)) get_pc () { return _builtin_return_address(0); }

void my_function() {
    void* pc = get_pc();
    asm volatile("": : :"memory");
    printf("Function pointer = %p, current pc = %p\n", &my_function, pc);
}
void main() {
    my_function();
}

If a function is not inlined, difference between the current value of the PC and value of the function pointer should small, otherwise it will be larger. On my system, when my_function is not inlined I get the following output:

Function pointer = 0x55fc17902500, pc = 0x55fc1790257b

If the function is inlined, I get:

Function pointer = 0x55ddcffc6560, pc = 0x55ddcffc4c6a

For the non-inlined version difference is 0x7b and for the inlined version difference is 0x181f.

0
  1. see the size of object files, they are different between inlined and not inlined
  2. use nm "obj_file" | grep "fun_name", they are also different
  3. gcc -Winline -O1
  4. compare with assembly code
1
  • Also, 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 is gcc-specific). Feb 24, 2017 at 12:27
-1

Above answer are very mush useful, I am just adding some point which we keep in our mind while writing inline function.

Remember, inlining is only a request to the compiler, not a command. Compiler can ignore the request for inlining. Compiler may not perform inlining in such circumstances like:

1) If a function contains a loop. (for, while, do-while)

2) If a function contains static variables.

3) If a function is recursive.

4) If a function return type is other than void, and the return statement doesn’t exist in function body.

5) If a function contains switch or goto statement.

Complete info: https://www.geeksforgeeks.org/inline-functions-cpp/

-1

The compiler does not make a function inline if the function returns an address.

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.