storage class, storage duration, file scope, lifetime, linkage confusion(存储类、存储持续时间、文件范围、生存期、链接混淆)
问题描述
当谈到一个C++/C
变量时,标准ISO文档提到了storage class
,storage duration
、file scope
、lifetime
和 linkage
.
When talked about a C++/C
variable, the standard ISO document mentions storage class
, storage duration
, file scope
, lifetime
, and linkage
.
我想了解基本线索以及它们之间的关系.但是读了ISO
文档后,我找不到这些概念的清晰逻辑图.
I want to understand the basic clues and how they are related to each other. But after reading the ISO
documents, I cannot find a clear logic map for these concepts.
你能帮忙澄清一下吗?我希望我能用这些概念来分析我的编程问题.
Could you help clarify it? I hope I can use there concepts to analyze my programming problem.
ISO 文档
部分:
- C99 6.7.1 存储类说明符
- C99 6.11.2 标识符的链接
- C++2003 7.1.1 存储类说明符
- C++2003 7.5 链接规范
- 等
推荐答案
存储类说明符是声明语法的
decl-specifier-seq
的一部分.它们控制由声明引入的名称的两个独立属性:它们的存储持续时间和它们的链接.存储类说明符:
Storage class specifiers are a part of the
decl-specifier-seq
of a declaration syntax. They control two independent properties of the names introduced by the declaration: their storage duration and their linkage. Storage class specifiers:
auto
- 自动存储持续时间(直到 C++11)register
- 自动存储时间.还提示编译器将变量放在处理器的寄存器中(已弃用)static
- 静态或线程存储持续时间和内部链接extern
- 静态或线程存储持续时间和外部链接thread_local
- 线程存储持续时间(C++11 起)
auto
- automatic storage duration (until C++11)register
- automatic storage duration. Also hints to the compiler to place the variable in the processor's register (deprecated)static
- static or thread storage duration and internal linkageextern
- static or thread storage duration and external linkagethread_local
- thread storage duration (since C++11)
存储持续时间:
自动
存储时间.该变量在封闭代码块的开头分配,并在结束时释放.所有非全局变量都有这个存储持续时间,除了那些声明的static
、extern
或thread_local
.static
存储时间.该变量在程序开始时分配,在程序结束时释放.该变量仅存在一个实例.所有全局变量都有这个存储持续时间,加上那些用static
或extern
声明的变量.thread
存储时间.该变量在线程开始时分配,在线程结束时释放.每个线程都有自己的变量实例.只有声明为thread_local
的变量具有此存储持续时间.thread_local
可以和static
或extern
一起出现来调整联动.(C++11 起)dynamic
存储时间.使用动态内存分配函数为每个请求分配和释放变量.
automatic
storage duration. The variable is allocated at the beginning of the enclosing code block and deallocated on end. All non-global variables have this storage duration, except those declaredstatic
,extern
orthread_local
.static
storage duration. The variable is allocated when the program begins and deallocated when the program ends. Only one instance of the variable exists. All global variables have this storage duration, plus those declared withstatic
orextern
.thread
storage duration. The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declaredthread_local
have this storage duration.thread_local
can appear together withstatic
orextern
to adjust linkage. (since C++11)dynamic
storage duration. The variable is allocated and deallocated per request by using dynamic memory allocation functions.
链接.表示对象、引用、函数、类型、模板、命名空间或值的名称可能具有链接.如果名称具有链接,则它引用与另一个作用域中的声明引入的相同名称的相同实体.如果在多个作用域中声明了一个变量、函数或另一个同名实体,但没有足够的链接,则会生成该实体的多个实例.
Linkage. A name that denotes object, reference, function, type, template, namespace, or value, may have linkage. If a name has linkage, it refers to the same entity as the same name introduced by a declaration in another scope. If a variable, function, or another entity with the same name is declared in several scopes, but does not have sufficient linkage, then several instances of the entity are generated.
识别以下链接:
- 无链接.该名称只能从其所在的范围内引用.
- 内部链接.可以从当前翻译单元的所有范围引用名称.
- 外部链接.可以从其他翻译单元中的范围引用该变量.
- No linkage. The name can be referred to only from the scope it is in.
- Internal linkage. The name can be referred to from all scopes in the current translation unit.
- External linkage. The variable can be referred to from the scopes in the other translation units.
参考.
这篇关于存储类、存储持续时间、文件范围、生存期、链接混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:存储类、存储持续时间、文件范围、生存期、链接混淆
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01