内联定义

发布于 2024-12-20 15:29:20 字数 847 浏览 2 评论 0原文

pg474,KNKing

“C99 中的一般规则是,如果 a 的所有顶级声明 特定文件中的函数包含内联但不包含外部,那么 该文件中函数的定义是内联的。”

  • 什么是:“函数的顶级声明”?

“如果该函数在程序中的任何地方使用(包括文件 包含其内联声明),然后是外部声明 该函数需要由其他文件提供。当 函数被调用时,编译器可能会选择执行普通调用 (使用函数的外部定义)或执行内联扩展 (使用函数的内联定义)。没有办法分辨哪个 编译器将做出的选择,因此这两个定义至关重要 保持一致。”

  • 他在这里说什么?

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

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

pg 473

“因此尝试从 另一个文件将被视为错误”

“因此,C99 对内联施加了以下限制 具有外部链接的功能(但不适用于具有内部链接的功能): 该函数不能定义可修改的静态变量。 该函数不能包含对具有内部变量的引用 链接。”

?如果一个函数是内联和外部,那么即使它确实声明了一个 静态整数我;由于该函数无法链接到您无法调用它, 但不会在内联函数之外创建静态变量 stack-frame - 所以你应该能够链接到它?执行内联函数 有堆栈框架吗?这是怎么回事?

pg474, K.N.King

"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."

  • What is a: "top-level declaration of a function"??

"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."

  • What is he saying here??

"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:

pg 473

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

"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."

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2024-12-27 15:29:20

我知道我写了很多,但我试图在解释您的文本的同时解释存储和链接的概念。希望这有帮助!

“C99 中的一般规则是,如果 a 的所有顶级声明
特定文件中的函数包含内联但不包含外部,那么
该文件中函数的定义是内联的。”

什么是:“函数的顶级声明”??

函数的声明的使用形式与变量的声明类似。它是声明函数的名称、返回类型和参数类型的单个语句。函数定义是函数的实际代码

int foo( int bar );

示例定义:

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

顶级。 函数声明只是一个声明在文件范围内(即,在任何块之外),这通常是所有函数声明所在的位置,尽管可以在其他函数内部声明和定义函数。

“如果该函数在程序中的任何地方使用(包括文件
包含其内联声明),然后是外部声明
该函数的功能需要由其他文件提供。当
函数被调用时,编译器可能会选择执行普通的
调用(使用函数的外部定义)或执行内联
扩展(使用函数的内联定义)。没有办法
告诉编译器将做出哪个选择,因此两者至关重要
定义保持一致。”

啊???他在这里说什么?

首先,什么是链接?变量或函数的链接定义了编译器将如何处理该对象的多个实例。没有链接的标识符总是即,程序中标识符的多个声明始终被视为单独/不同的实体,并且函数参数和局部变量没有链接。这是同一个实体。默认情况下,全局标识符具有外部链接,例如,如果程序的两个源文件中有全局变量“int x;”,它们将被链接在一起并被视为相同。内部链接意味着一个源文件中的所有标识符声明都引用单个实体,但其他源文件中相同标识符的声明引用不同的实体,这是使文件“私有”的 C 方式。 。这是文件范围内的 C 关键字“static”。

现在,回到该段落。一个函数不能被定义多次。因此,想要使用其他源文件中的函数的源文件需要包含外部声明(这是进行函数调用所需的信息)。本段解释的是当文件具有内联函数的外部声明时会发生什么。编译器必须选择是否应该获取内联定义并将其插入到调用函数的位置,或者是否应该保留外部链接,使执行像平常一样跳转到代码文本;并且无法预测编译器将做出什么选择。

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

但我认为你不能调用具有外部链接的函数!这
编译器会给出错误:pg 473“因此尝试调用平均值
来自另一个文件的将被视为错误”

如果您无法调用在不同源文件中定义的函数(即外部链接函数),那么 C 确实是一种非常弱且无聊的语言!

“因此,C99 对内联施加了以下限制
具有外部链接的功能(但不适用于具有内部链接的功能):
该函数不能定义可修改的静态变量。功能
不得包含对具有内部链接的变量的引用。”

为什么?如果一个函数是内联函数和外部函数,那么即使它确实声明了
静态 int i;由于该函数无法链接到您无法调用
它,但不会在内联之外创建静态变量
函数堆栈框架 - 所以你应该能够链接到它?做内联
函数有栈帧吗?这是怎么回事??

静态存储变量是不属于执行堆栈的变量。它的空间在程序开始运行之前分配一次,并在整个执行过程中存在。它们保留其初始值,直到分配不同的值为止。全局变量(文件范围)默认静态存储。这与自动存储的变量形成对比,自动存储的变量在程序执行进入声明它们的块之前在堆栈上分配,并在执行离开该块时被丢弃。局部变量(块作用域)默认是自动的。

回到问题:内联函数内部静态存储的变量有什么问题?函数内静态存储的变量存在的假设是该函数只有一个定义,因此该静态变量也只有一个定义。但根据定义,内联是函数定义的重复,因此您不需要在函数调用期间跳转代码文本。如果函数中存在静态变量,那么您将不得不跳转到其存储位置,从而违背了内联的目的,即拥有“就在那里”的所有内容的副本。解决方案:要求变量不可修改,以便编译器可以内联永久值。

关于你的最后一个问题:内联函数确实有一个堆栈帧:它与调用函数相同的堆栈帧,因为正在复制内联函数的代码文本,以避免正常外部函数跳转的标准指令开销。

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!

"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."

What is a: "top-level declaration of a function"??

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.

Example declaration:

int foo( int bar );

Example definition:

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

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.

"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??

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.

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"

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"

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!

"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."

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 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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文