C ++中何时将内存分配给静态变量 [英] When is memory allotted to static variables in C++

查看:97
本文介绍了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).

存储类别:

Static 变量在程序启动时以实现定义的方式分配,并且一直存在,直到程序结束.它们不能被释放"或重新分配". (典型的实现是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).如果您未指定初始值,则它们将在已翻译的订单"中进行默认初始化. (对于基本类型,这仅意味着填充零).

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相反,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.

这篇关于C ++中何时将内存分配给静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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