内联定义 [英] Inline Definitions

查看:89
本文介绍了内联定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pg474,K.N.King

在C99的一般规则是,如果的所有顶层声明
  在一个特定的文件中的函数包括内联而不是extern,这样
  在该文件中的函数的定义是内联。

"The general rule in C99 is that if all top-level declarations of a function in a particular file include inline but not extern, then the definition of the function in that file is inline."


  • 什么是功能的顶级声明??

如果功能是在程序任意位置使用(包括文件
  这containts其内嵌的声明),则外部声明
  该函数将需要由某些其它文件来提供。当。。。的时候
  函数被调用时,编译器可能选择执行普通的呼叫
  (使用功能的外部定义)或执行内联展开
  (使用功能的内联的定义)。有没有办法知道哪些
  选择编译器会做,所以这是至关重要的两个定义
  是一致的。

"If the function is used anywhere in the program (including the file that containts its inline declaration), then an external declaration of the function will need to be provided by some other file. When the function is called, the compiler may choose to perform an ordinary call (using the function's external definition) or perform inline expansion (using the function's inline definition). There's no way to tell which choice the compiler will make, so it's crucial that the two definitions be consistent."


  • 他在说什么?在这里

具有静态存储持续时间的变量是一个特殊的问题
  与外部联动内联函数

"Variables with static storage duration are a particular problem for inline functions with external linkage"

但我认为你不能调用一个函数具有外部链接!该
编译器会给出错误:

But i thought you couldn't call a function with external linkage! The compiler would give an error:

皮克473

所以试图从平均调用
  另一个文件会被认为是一个错误

"so attempting to call average from another file will be considered an error"

因此,C99强加内嵌以下限制
  外部链接功能(而不是在一个具有内部链接):
    该功能可以不定义修改的静态变量。
    该功能可能不包含与内部变量的引用
    联动。

"Consequently, C99 imposes the following restrictions on an inline function with external linkage (but not on one with internal linkage): The function may not define a modifiable static variable. The function may not contain references to variables with internal linkage."

为什么?如果一个函数的 inline和extern ,然后即使它声明
静态INT I;因为函数无法联系到你不能调用它,
但不会一个静态变量内联函数外创建
堆栈帧 - 所以你应该可以链接到它?做内联函数
有一个堆栈帧?这是怎么回事?这里

Why?? If a function is inline and extern, then even if it does declare a static int i; since the function can't be linked to you can't call it, but won't a static variable be created outside the inline functions stack-frame - so you should be able to link to it? Do inline functions have a stack frame? What's going on here??

推荐答案

我知道我写了很多,但我试图解释存储和联动的概念,沿着我的文字解释的一面。希望这有助于!

I know I wrote alot, but I tried to explain the concepts of storage and linkage along side of my explanations of your text. Hope this helps!

在C99的一般规则是,如果的所有顶层声明
  在一个特定的文件中的函数包括内联而不是extern,这样
  在该文件中的函数的定义是内联。

"The general rule in C99 is that if all top-level declarations of a function in a particular file include inline but not extern, then the definition of the function in that file is inline."

什么是:??

函数的声明以类似的形式被用于一个变量的声明。这是声明的名称,返回类型和参数类型的函数的单个语句。一个函数的定义是函数的实际code。

The declaration of a function is used in similar form to the declaration of a variable. It's a single statement that declares the name, return type, and parameter types of a function. A function definition is the actual code of the function.

声明示例:

int foo( int bar );

例定义:

int foo( int bar ){
    return -bar;
}

A 顶级函数的声明仅仅是声明这是在在文件范围(即,任何块外)。这通常是,所有函数声明,虽然它是可能的声明和定义等功能内部的功能。

A top-level function declaration is simply a declaration that's at the file scope (i.e., outside of any block). This is typically where all function declarations are, though it is possible to declare and define functions inside of other functions.

如果功能是在程序任意位置使用(包括文件
  这containts其在线声明),则外部声明
  的函数将需要由某些其它文件来提供。当。。。的时候
  函数被调用时,编译器可以选择执行一个普通
  调用(使用功能的外部定义)或执行内联
  扩展(使用功能的内嵌定义)。有没有办法
  告诉编译器会做出何种选择,因此它是至关重要的,这两个
  定义是一致的。

"If the function is used anywhere in the program (including the file that containts its inline declaration), then an external declaration of the function will need to be provided by some other file. When the function is called, the compiler may choose to perform an ordinary call (using the function's external definition) or perform inline expansion (using the function's inline definition). There's no way to tell which choice the compiler will make, so it's crucial that the two definitions be consistent."

咦?他在说什么?在这里

Huh??? What is he saying here??

首先,是什么联系?一个变量或函数的定义联动编译器会如何对待该对象的多个实例。没有连接的标识符总是'个人'。也就是说,在程序中标识符的多重声明总是被视为单独的/不同的实体。功能参数和局部变量没有联动。是外部链接到一个标识符的所有引用指的是同一实体。这是C关键字的extern。默认情况下,全局标识符有外部链接。这意味着,例如,如果有全局变量整数X;在该方案的两个源文件,它们将被连接在一起,并作为同一变量处理。内部链接表示一个源文件中的标识符的所有声明指单一实体,但在其它的源文件中的相同标识符的声明是指不同的实体。这是使事情私人到一个文件中C方式。这是C关键字静态,在文件的范围。

First of all, what is linkage? The linkage of a variable or function defines how the compiler will treat multiple instances of that object. Identifiers that have no linkage are always 'individuals'. That is, multiple declarations of the identifier within the program are always treated as separate/distinct entities. Function parameters and local variables have no linkage. All references to an identifier with external linkage refer to the same entity. This is the C keyword 'extern'. By default, global identifiers have external linkage. This means that, for example, if you have the global variable "int x;" in two source files of the program, they will be linked together and treated as the same variable. Internal linkage means that all declarations of the identifier within one source file refer to a single entity, but declarations of the same identifer in other source files refer to different entities. This is the C way of making things "private" to a file. This is the C keyword 'static', in the file scope.

现在,回款。函数不能被定义一次以上。这样要使用的功能其他源文件的源文件需要包括一个外部声明(这是使函数调用所需的信息)。这是什么款解释是关于当一个文件都有一个外部声明的内联函数时会发生什么。编译器有选择它是否应该取内联定义和插入函数被调用的地方,或者是否应该preserve外部联动,使执行跳转到code文本像正常;有没有办法predict选择什么,编译器将做出。

Now, back to the paragraph. A function cannot be defined more than once. So source files that want to use functions from other source files need to include an external declaration (which is the information needed to make the function call). What this paragraph is explaining is about what happens when a file has an external declaration to an inline function. The compiler has to choose whether or not it should fetch the inline definition and insert it where the function is being called, or if it should preserve the external linkage, making the execution jump to the code text like normal; and there is no way to predict what choice the compiler will make.

具有静态存储持续时间的变量是一个特殊的问题
  与外部联动内联函数

"Variables with static storage duration are a particular problem for inline functions with external linkage"

但我认为你不能调用一个函数具有外部链接!该
  编译器会给出错误:PG 473,因此试图调用平均
  从另一个文件将被视为错误

But i thought you couldn't call a function with external linkage! The compiler would give an error: pg 473 "so attempting to call average from another file will be considered an error"

如果你不能调用在不同的源文件(即外部链接的功能)中定义的函数,C是一个非常薄弱的​​,乏味的语言真的!

If you couldn't call a function that's defined in a different source file (i.e., an externally linked function), C would be a very weak and boring language indeed!

因此,C99强加内嵌以下限制
  外部链接功能(而不是在一个具有内部链接):
  该功能可以不定义修改的静态变量。功能
  不得包含与内部链接的变量的引用。

"Consequently, C99 imposes the following restrictions on an inline function with external linkage (but not on one with internal linkage): The function may not define a modifiable static variable. The function may not contain references to variables with internal linkage."

为什么?如果一个函数是inline和extern,那么即使它声明
  静态INT I;因为函数无法联系到你不能打电话
  它,但不会静态变量内嵌外创建
  功能堆栈帧 - 所以你应该可以链接到它?做内联
  函数有一个堆栈帧?这是怎么回事?这里

Why?? If a function is inline and extern, then even if it does declare a static int i; since the function can't be linked to you can't call it, but won't a static variable be created outside the inline functions stack-frame - so you should be able to link to it? Do inline functions have a stack frame? What's going on here??

A 存储静态变量是一个不执行堆栈的一部分。它的空间被分配一次,程序开始运行之前,并在整个执行存在。他们保留无论其初始值是直到一个不同的值分配。全局变量(文件范围)静态默认保存。这是在对比的自动存储变量,它们被分配执行程序进入它们所声明的块之前的堆栈,并且在执行离开该块被丢弃。局部变量(块范围),默认情况下自动进行的。

A statically stored variable is one that is not part of the execution stack. Its space is allocated once, before the program begins to run, and exists throughout the entire execution. They retain whatever their initial value was until a different value is assigned. Global variables (file scope) are statically stored by default. This is in contrast to automatically stored variables, which are allocated on the stack just before program execution enters the block in which they are declared, and which are discarded when execution leaves that block. Local variables (block scope) are automatic by default.

回过头来看看这个问题:用一个静态变量存储什么是内联函数内部的问题呢?函数内的静态地存储变量住假设仅存在一个该函数的定义,因此,仅一个定义,静态变量的下。但是,内联,顾名思义,就是函数定义的重复,这样你就不需要一个函数调用期间围绕code-文本跳。如果一个静态变量的函数存在,那么你就必须跳转到它的存储位置,击败内联,这是拥有一切的副本那里的宗旨。解决办法:要求该变量不可修改,这样编译器可以内联恒值

Going back to the question: what's the problem with a statically stored variable inside of an inline function? A statically stored variable within a function lives under the assumption that there is only one definition of that function, and therefore only one definition of that static variable. But inlining, by definition, is a repetition of the function definition so that you don't need to jump around the code-text during a function call. If a static variable existed in the function, then you would have to jump to its storage location, defeating the purpose of inlining, which is to have a copy of everything "right there". The solution: require that the variable not be modifiable, so that the compiler can inline the permanent value.

在你最后一个问题:内联函数确实有一个堆栈帧:它的调用函数相同的堆栈帧时,因为内联函数的code-文本是为了避免正常的外部标准的指令开销被复制功能跳跃。

On your last question: inline function's do have a stack frame: it's the same stack frame of the calling function, because the inline function's code-text is being copied in order to avoid the standard instruction overhead of normal external function jumping.

这篇关于内联定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆