C语言 内联函数与宏函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13375288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:25:14  来源:igfitidea点击:

inline function vs macro function

cmacrosinline

提问by MOHAMED

Possible Duplicate:
Inline functions vs Preprocessor macros

可能的重复:
内联函数与预处理器宏

I want to know the difference between the inline function and macro function.

我想知道内联函数和宏函数之间的区别。

1) is inline function is the same of macro function ?

1) 内联函数是否与宏函数相同?

2) I know that both are not called but they are replaced by its code in the compilation phase. is not?

2)我知道两者都没有被调用,但它们在编译阶段被其代码替换。不是?

3) If there is difference, Could you specify it?

3)如果有差异,你能说明吗?

回答by iabdalkader

Inline replaces a call to a function with the body of the function, however, inline is just a requestto the compiler that could be ignored (you could still pass some flags to the compiler to force inline or use always_inlineattribute with gcc).

内联用函数体替换对函数的调用,但是,内联只是对编译器的一个可以被忽略的请求(您仍然可以将一些标志传递给编译器以强制内联或在 gcc 中使用always_inline属性)。

A macro on the other hand, is expanded by the preprocessorbefore compilation, so it's just like text substitution, also macros are not type checked, inline functions are. There's a comparison in the wiki.

另一方面,宏在编译前被预处理器扩展,所以它就像文本替换一样,宏也没有类型检查,内联函数是。维基上有对比。

For the sake of completeness, you could still have some kind of type safety with macros, using gcc's __typeof__for example, the following generate almost identical code and both cause warnings if used with the wrong types:

为了完整起见,您仍然可以使用宏进行某种类型安全,__typeof__例如使用 gcc ,以下生成几乎相同的代码,如果使用错误的类型,两者都会导致警告:

#define max(a,b) \
  ({ __typeof__ (a) _a = (a); \
      __typeof__ (b) _b = (b); \
    _a > _b ? _a : _b; })

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

Note: sometimes typeless macros are just what's needed, for example, have a look at how uthashuses macros to make any C structure hashable without resorting to casts.

注意:有时无类型宏正是所需要的,例如,看看uthash如何使用宏使任何 C 结构可散列,而无需求助于强制转换。

回答by Lelanthran

1) No.

1) 没有。

2) A Macro in C is simply text that is expanded beforethe compiler processes the source code. The inline keyword is used as a hint to the compiler that the function can be placed inline without the need for a call-stack to be set up.

2) C 中的宏只是在编译器处理源代码之前展开的文本。inline 关键字用于提示编译器,该函数可以内联放置,而无需设置调用堆栈。

So, for example, lets say that you got the following two snippets of code (First the macro and then the inline function):

因此,例如,假设您有以下两个代码片段(首先是宏,然后是内联函数):

#define MAX(x,y)     (x > y ? x : y)

and

inline int max(int x, int y) { return x > y ? x : y; }

When the preprocessor finds the following call in your code:

当预处理器在您的代码中发现以下调用时:

int highest = MAX (var_1, var_2);

it replaces it with

它用

int highest = (var_1 > var_2 ? var_1 : var_2);

The above is what the compiler eventually gets during the compilation process, hence the snippet defined by MAX(x,y) is the replacementfor MAX(var_1, var_2). When the compiler finds the function call

以上是编译器在编译过程中最终得到的,因此 MAX(x,y) 定义的代码片段是MAX(var_1, var_2)的替代。当编译器发现函数调用时

int highest = max (var_1, var_2);

Then the function "max" gets called. You must assume that it gets called the normal way, because the compiler is free to ignore your "inline" hint and make calls to the function go through the normal call-stack rather than simply placing the code for the function in the place it is encountered.

然后调用函数“max”。您必须假设它以正常方式被调用,因为编译器可以自由地忽略您的“内联”提示并通过正常的调用堆栈调用该函数,而不是简单地将函数的代码放在它所在的位置遭遇。

One last caveat with macros: because it is all text replacement and not code replacement, if you do something like this:

宏的最后一个警告:因为它是所有文本替换而不是代码替换,如果您执行以下操作:

int highest = MAX (v1++, v2++);

the preprocessor will expand that to:

预处理器将其扩展为:

int highest = (v1++ > v2++ ? v1++ : v2++);

which is probably not what you intended.

这可能不是你想要的。

回答by WhozCraig

Macros are declarations that are substituted by the preprocessor (before the actual compile) The are not functions at all. Before the actual compilephase commences the macros are long-gone, and all that remains is their expansions (including expansion to nothing).

宏是由预处理器替换的声明(在实际编译之前)。根本不是函数。在实际的编译阶段开始之前,宏早已不复存在,剩下的就是它们的扩展(包括无扩展)。

Inline functions are, in fact, language-compliant functions, with scope rules, variable declarations, logic constructs (loops, etc), and so on. An inline function is not expanded pre-compile-step like macros. They are compiledjust as regular code is, but can be then injected (for lack of a better term) into compiled code and optimized as needed.

内联函数实际上是符合语言的函数,具有范围规则、变量声明、逻辑结构(循环等)等。内联函数不像宏那样在预编译步骤中展开。它们像常规代码一样被编译,但可以被注入(因为没有更好的术语)到编译代码中并根据需要进行优化。

回答by Omkant

No inlineis not same as macrodefinition, as macro is preproccessed but inline is just the indication to the compiler to put the whole body of the function where it is called.

Noinlinemacro定义不同,因为宏是预先处理的,但内联只是指示编译器将整个函数体放在调用它的地方。

putting inline keyword before any function definition is just the indication or request , compiler is free to choose whether it's going to make that function inline or leave it as normal

将 inline 关键字放在任何函数定义之前只是指示或请求,编译器可以自由选择是将该函数内联还是保持正常

macro substitution : preprocessing phase(i.e. before compilation )

宏替换:(preprocessing phase即编译前)

-- input file test.cresult file in linux test.i

-- test.clinux中输入文件结果文件test.i

inline substitution : compilation phase

内联替换: compilation phase