c++ - 存储类别、存储持续时间、文件范围、生命周期、链接困惑

标签 c++ static scope

当谈论C++/C时变量,标准 ISO 文档提到 storage class , storage duration , file scope , lifetime ,和linkage .

我想了解基本线索以及它们之间的关系。但读完ISO后文档中,我找不到这些概念的清晰逻辑图。

你能帮忙澄清一下吗?我希望我可以使用这些概念来分析我的编程问题。

ISO documents部分:

  • C99 6.7.1 存储类说明符
  • C99 6.11.2 标识符的链接
  • C++2003 7.1.1 存储类说明符
  • C++2003 7.5 链接规范
  • 等等

最佳答案

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 - 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 linkage
  • extern - static or thread storage duration and external linkage
  • thread_local - thread storage duration (since C++11)

Storage durations:

  • 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 declared static, extern or thread_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 with static or extern.
  • 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 declared thread_local have this storage duration. thread_local can appear together with static or extern 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.

The following linkages are recognized:

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

Reference.

关于c++ - 存储类别、存储持续时间、文件范围、生命周期、链接困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17784519/

相关文章:

c++ - 在 C++ 中创建蛮力算法

c++ - 结构化绑定(bind)和基于范围的;在 gcc 中抑制未使用的警告

javascript - 如何从 html 可访问标记使用或创建非全局 JavaScript 变量缓存

ruby-on-rails - 如何通过抽象事件记录向子类类添加范围

c++ - Qt (c++) - 是否可以使用信号从方法返回?

c++ - STL for_each 无法在 Xcode 中使用 map

c++ - 在函数静态变量上调用 setter 一次

c++ - 使用内联函数定义静态二维数组

javascript - 如何在父类(super class)静态函数中引用子类的静态参数?

c - 如何在机器级别或内存级别的编译器中实现变量的范围