Questions tagged [inline-functions]

By using keyword 'inline' in function definition, programmer can request that the (C/C++) compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined.

From wikipedia:

In various versions of the C and C++ programming languages, an inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. (However, compilers are not obligated to respect this request.)

Typical inline-function in C++:

inline int getLength() const { return _length; }

Typical inline-function in C:

inline int max(int a, int b) 
{ 
    return (a > b) ? a : b;
}

See wiki page for more info.

174 questions
19
votes
4 answers

Inlining of vararg functions

While playing about with optimisation settings, I noticed an interesting phenomenon: functions taking a variable number of arguments (...) never seemed to get inlined. (Obviously this behavior is compiler-specific, but I've tested on a couple of…
Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
16
votes
2 answers

C: Pointer to inline function

I have a static inline function defined in an H file, and at one point in a C file, I'm assigning a pointer to the function, something like this: foo.h: static inline void frobnicate(void) { // frobs something. } foo.c #include "foo.h" void…
brianmearns
  • 9,581
  • 10
  • 52
  • 79
15
votes
2 answers

Is GCC's option -O2 breaking this small program or do I have undefined behavior

I found this problem in a very large application, have made an SSCCE from it. I don't know whether the code has undefined behavior or -O2 breaks it. When compiling it with gcc a.c -o a.exe -O2 -Wall -Wextra -Werror it prints 5. But it prints 25…
Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
13
votes
3 answers

Does LLVM convert Objective-C methods to inline functions?

Does LLVM automatically convert Objective-C methods to inline functions when possible? (I.e., is it just as performant to create an Objective-C method for a block of code that you could otherwise paste inline?) If LLVM doesn't perform this…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
13
votes
1 answer

Multiple definition of inline functions when linking static libs

I have a C++ program that I compile with mingw (gcc for Windows). Using the TDM release of mingw which includes gcc 4.4.1. The executable links to two static library (.a) files: On of them is a third-party library written in C; the other is a C++…
Dennis
  • 2,607
  • 3
  • 21
  • 28
11
votes
2 answers

Tool to automatically inline JavaScript function calls?

Inlining JavaScript function calls speeds up the execution and also reduces the code size after gzipping, as described in this article: http://blog.calyptus.eu/seb/2011/01/javascript-call-performance-just-inline-it/ However, I can't find a tool…
Gary Chang
  • 1,042
  • 12
  • 18
11
votes
2 answers

Is there an actual example where inline is detrimental to the performance of a C program?

In many debates about the inline keyword in function declarations, someone will point that it can actually make your program slower in some cases – mostly due to code explosion, if I am correct. I have never met such an example in practice myself.…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
10
votes
3 answers

Crash when running application due to existence of unexecuted code in source file - c++

I'm working on a pretty tricky problem that I've been on for literally a week now. I've hit a very hard wall and my forehead hurts from banging it so I'm hoping someone can help me out. I am using Visual Studio 2005 for this project - I have 2008…
Liron
  • 2,012
  • 19
  • 39
10
votes
3 answers

Why are C++ methods sometimes defined inside classes?

I frequently run into large, non-template classes in C++ where simple methods are defined directly in the class body in the header file instead of separately in the implementation file. For example: class Foo { int getBar() const { return bar; } …
Jay Conrod
  • 28,943
  • 19
  • 98
  • 110
10
votes
2 answers

Possible to call inline functions in gdb and/or emit them using GCC?

We all know that inline functions can make debugging trickier, as they can be elided from stack traces etc. But suppose I want to call an inline function from within gdb, and I know its name and its arguments. I think I should be able to do that,…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
9
votes
9 answers

Inlining C++ code

Is there any difference to the following code: class Foo { inline int SomeFunc() { return 42; } int AnotherFunc() { return 42; } }; Will both functions gets inlined? Does inline actually make any difference? Are there any rules on when you…
Rob
  • 76,700
  • 56
  • 158
  • 197
9
votes
2 answers

What's is the idea behind C99 inline?

I am confused about inline in C99. Here is what I want: I want my function get inlined everywhere, not just limited in one translation unit (or one compilation unit, a .c file). I want the address of the function consistent. If I save the address…
zsh
  • 91
  • 3
9
votes
2 answers

What happens if we make recursive functions as inline?

I have a doubt regarding inline functions. Inline functions will not involve any function calls but just replacement of function definition wherever the call is made to the inline function.Inline functions have type enforcement unlike macros. What…
gst
  • 1,251
  • 1
  • 14
  • 32
8
votes
3 answers

C inline functions and "undefined external" error

I'm trying to replace some macro subroutines with inline functions, so the compiler can optimize them, so the debugger can step into them, etc. If I define them as normal functions it works: void do_something(void) { blah; } void main(void) { …
endolith
  • 25,479
  • 34
  • 128
  • 192
8
votes
1 answer

How do I force OCaml to inline a function?

Is it possible to tell the OCaml compiler to inline a function, instead of hoping that its optimization process will do so itself?
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
1
2
3
11 12