c inline

作者: stanley_shen | 来源:发表于2020-06-17 14:39 被阅读0次

A C code can be optimized in two ways: For Code size and for Execution Time.

inline functions:

gcc.gnu.org says,

By declaring a function inline, you can direct GCC to make calls to that function faster. One way GCC can achieve this is to integrate that function's code into the code for its callers. This makes execution faster by eliminating the function-call overhead; in addition, if any of the actual argument values are constant, their known values may permit simplifications at compile time so that not all of the inline function's code needs to be included. The effect on code size is less predictable; object code may be larger or smaller with function inlining, depending on the particular case.

So, it tells the compiler to build the function into the code where it is used with the intention of improving execution time.

If you declare Small functions like setting/clearing a flag or some bit toggle which are performed repeatedly, inline, it can make a big performance difference with respect to time, but at the cost of code size.


non-static inline and Static inline

Again referring to gcc.gnu.org,

When an inline function is not static, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non-static inline function is always compiled on its own in the usual fashion.


extern inline?

Again, gcc.gnu.org, says it all:

If you specify both inline and extern in the function definition, then the definition is used only for inlining. In no case is the function compiled on its own, not even if you refer to its address explicitly. Such an address becomes an external reference, as if you had only declared the function, and had not defined it.

This combination of inline and extern has almost the effect of a macro. The way to use it is to put a function definition in a header file with these keywords, and put another copy of the definition (lacking inline and extern) in a library file. The definition in the header file causes most calls to the function to be inlined. If any uses of the function remain, they refer to the single copy in the library.


To sum it up:

  1. For inline void f(void){}, inline definition is only valid in the current translation unit.
  2. For static inline void f(void) {} Since the storage class is static, the identifier has internal linkage and the inline definition is invisible in other translation units.
  3. For extern inline void f(void); Since the storage class is extern, the identifier has external linkage and the inline definition also provides the external definition.
引用自 What is the use of the inline keyword in C?

相关文章

  • c inline

    A C code can be optimized in two ways: For Code size and ...

  • c++   inline

    在C中,编译器使用宏定义节省编译时间。在C++中使用内联函数来实现同样的效果。在程序编译时,编译器会将内联函数调用...

  • 关于元素居中

    水平居中: 内联元素(inline,inline-block): 在父级元素上添加样式:text-align :c...

  • C++基础

    C++ readme 避免头文件重复定义, extern C的用法: 内联函数: inline function ...

  • 2018-03-07

    关于c++的inline关键字,以下说法正确的是() 正确答案: D 你的答案: A(错误) A使用inline关...

  • Function Macros

    Avoid function macros. In C++,inline functions render fun...

  • 高质量C++编程指南 ----C++函数的高级特性

    8. C++函数的高级特性 对比于C 语言的函数,C++增加了重载(overloaded)、内联(inline)、...

  • 第一周(Geek Band)

    C++实现数据和函数的封装 C++面向对象(ObjectOriented) 基本格式 Inline内联函数关键字 ...

  • 宏、函数、inline函数的性能对比

    [todo]:inline的耗时比预期多。 代码:https://github.com/gykimo/c_plus...

  • 《Effective C++中文版》目录

    让自己习惯C++ 1、视C++为一个语言联邦 2、尽量以const、enum、inline替换#define 3、...

网友评论

      本文标题:c inline

      本文链接:https://www.haomeiwen.com/subject/srksxktx.html