什么时候内存分配给 C++ 中的静态变量
我是 C++ 的新手,遇到了一个问题.
I am a newbie to C++ and facing a problem.
我在一本书中读到,一旦创建了该类的对象,就会将内存分配给静态变量.现在,如果我将这个静态变量设为全局怎么办?这种情况下什么时候初始化?
I read in a book that memory is allotted to a static variable, once the object is created of that class. Now, what if I make this static variable global ? When would be it initialized in that case ?
另外,我还在一些文章中读到静态变量分配在堆上并且它们不依赖于对象的构造......这是真的吗?如果是,请给我解释一下内存初始化步骤,我需要帮助.
Plus, I have also read in some articles that static variables are allotted on heap and they dont rely on the construction of objects...is this true ? If yes, please explain the memory initialization steps to me, I need help.
非常感谢!
推荐答案
首先:停止思考 C 和 C++ 中的全局变量,否则您将永远处于混乱状态.这个问题比 Python 或 Pascal 等问题更复杂,因此您不能只使用一个词来表示概念.
First: STOP THINKING ABOUT GLOBAL VARIABLES IN C AND C++, or you will perpetuate your state of confusion. The issue is more complex than in, e.g., Python or Pascal, and therefore you can't just use a single word for the concept(s).
其次,没有堆"或栈"――这些是操作系统和 CPU 的细节,与抽象的 C++ 语言规范无关.
Second, there is no "heap" or "stack" -- these are operating system and CPU details, and have nothing to do with the abstract C++ language specification.
现在,变量具有 1) 作用域、2) 链接和 3) 存储类.static 关键字用于影响所有三个方面,具体取决于您使用它的位置.
Now, variables have 1) scope, 2) linkage and 3) storage class. The static keyword is used to affect the all three aspects, depending on where you use it.
作用域:声明变量的位置.如果在函数内,则为函数作用域,如果在函数外,则为文件作用域(您可能将其称为全局变量").
Scope: where the variable is declared. If within a function, it is a function-scope, if outside of a function, it is file-scope (what you probably refer to as "global variable").
链接:变量是否可以从其他编译单元访问(当您的程序包含多个源文件时相关).
Linkage: whether the variable is accessible from other compilation units (relevant when your program consists of multiple source files).
存储类:
静态变量在程序启动时以实现定义的方式分配,它们一直存在到程序结束.它们不能被释放"或重新分配".(典型的实现是其他人提到的 BSS 和 DATA 段).
Static variables are allocated in implementation-defined manner upon program startup, and they live until the program ends. They cannot be "freed" or "reallocated". (a typical implementation are BSS and DATA segments as others have mentioned).
自动变量仅存在于函数范围内,它们在函数入口(通常在 CPU 的堆栈上)分配(并且可能初始化)并在函数退出时释放.
Automatic variables exist only at function scope, they are allocated (and possibly initialized) on function entry (usually on the CPU's stack) and deallocated on function exit.
动态 存储类可能是您所指的堆".这些是通过 malloc/free 或 new/delete 直接操作存储的变量.静态变量的存储与动态存储的管理方式非常不同,这两种存储从根本上是不兼容的.
Dynamic storage class is what you probably refer to "the heap". Those are variables whose storage is directly manipulated through malloc/free or new/delete. Storage for static variables is managed in a VERY different way than dynamic storage, and the two kinds of storage are fundamentally incompatible.
例子:
===
// ALL file-scope variables have static storage class
int global1; // file-scope, external linkage
static int global2; // file-scope, internal linkage
void f()
{
static int x; // static storage class, function-scope
int y; // automatic storage class, function-scope
free(&x); // ILLEGAL, WILL CRASH!
free(&y); // DITTO!
free(&global1); // DITTO!
}
===
现在,所有具有静态存储类(global1、global2 和 x)的变量都在程序启动之前分配和初始化.如果您不指定初始值,它们将在 UNSPECIFIED ORDER 中默认初始化.(对于原始类型,这只是意味着用零填充).
Now, all variables with static storage class (global1, global2 and x) are allocated and initialized before program startup. If you do not specify initial values, they are default-initialized in UNSPECIFIED ORDER. (For primitive types, this just means filling with zeros).
静态变量仅在程序退出时被释放.这意味着,函数 f 中的x"将仅初始化一次(在程序启动时),并且它将在函数调用之间保留其值(而不是 y,它将在每个函数条目上分配并在每个函数处释放退出,因此它的价值也被破坏了).请注意,在函数中使用静态变量与多线程和重入非常不兼容,除非您非常清楚自己在做什么.
Static variables are deallocated only at program exit. This means, that 'x' in function f will be initialized ONLY ONCE (at program startup) and that it will RETAIN ITS VALUE between invocations of the function (as opposed to y which will be allocated on each function entry and deallocated at each function exit, thus also its value destroyed). Note that using static variables within functions is very incompatible with multithreading and reentrancy, unles you know very well what you're doing.
相关文章