2

Consider the follwing C++ code:

#include <iostream>

using namespace std;

class A
{
    int a;
    public:
        A();
        void f();
};

A::A()
{
    cout<<"Constructor"<<endl;
}

inline void A::f()
{
    cout << "hello\n";  
}

int main()
{
    A a;
    a.f();
    a.f();
    a.f();
    return 0;
}

Here the function f is made inline by me. Now I ran the size command to find out the size of the text section. I got the following output:

text       data     bss     dec     hex filename
2148        312     144    2604     a2c ./1

Now I made the function f non-inline by removing the inline keyword from its definition. The I again ran the size command:

text       data     bss     dec     hex filename
2148        312     144    2604     a2c ./1

So, the size of the text section is same in both cases, although I expected the size to be greater in case of f being inline as its call is simply replaced by the code in case of inline.

So, what could be the reason for this? Is there any example where the size will change?

tapananand
  • 4,392
  • 2
  • 19
  • 35
  • `inline` is a suggestion, there is no guarantee the compiler will actually inline your function. – Red Alert Nov 14 '14 at 17:41
  • @CaptainObvlious: But if the code size increases, the increase will be visible. What does it have to do with alignment?? – tapananand Nov 14 '14 at 17:42
  • Your compiler probably ignores `inline`, at least as far as deciding whether to generate code for a function inline or not (though `inline` also affects the one-definition rule in ways it can't ignore). BTW, nearly a dupe of: http://stackoverflow.com/q/1759300/179910 – Jerry Coffin Nov 14 '14 at 17:43
  • 1
    No, it doesn't necessarily have to increase the size of the text segment. If your code is 1000 bytes and the text segment is aligned to 2k you'll have 1000+ empty bytes at the end. If the next time you compile and the size of the code is 1500 bytes you'll have 500+ empty bytes at the end. The code grows in size but the page size remains the same. – Captain Obvlious Nov 14 '14 at 17:55

4 Answers4

3

inline doesn't affect whether or not a function call is inlined (although it might, or might not, influence the compiler's decision). The compiler is free to inline any function call, under the "as-if" rule that any optimisation is permitted as long as it doesn't change the program's behaviour.

The purpose of inline is to allow you to define the function in a header, giving multiple definitions (one in each translation unit that includes the header), which would otherwise be forbidden by the One Definition Rule. For some compilers, this is necessary to allow a function call to be inlined - unless it has a link-time optimiser, the compiler can only inline calls to functions when the definition is available.

If you're using GCC, then there's an attribute you can use to prevent inlining, if you want to see the effect:

void f() __attribute__ ((noinline));
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

Since you have all code in one file, most likely the compiler inlined A::f in both cases.

Here is a demo showing this on your code.

Better than checking text section size, you can specify -S compiler option and compare the assembly outputs for both versions.

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
1

This is probably the case of link time optimization in this case. This is proved by the fact that on compiling the code using g++ -S 1.cpp The size of assembly code with inline is larger than in the case of non-inline. So, the actual behavior is seen in assembly but not in final executable after link time.

tapananand
  • 4,392
  • 2
  • 19
  • 35
0

inline is just a request to the compiler. It may or may not execute the function as inline.

Venkatesh
  • 1,537
  • 15
  • 28
  • These days, it is not even a request. Modern compilers limit the effect of `inline` specifier to relaxing the ODR only. As to actual inlining, they prefer they own judgment. – ach Nov 14 '14 at 17:50