Is global memory initialized in C++?(全局内存是否在 C++ 中初始化?)
问题描述
全局内存是否在 C++ 中初始化?如果是这样,如何?
Is global memory initialized in C++? And if so, how?
(第二)澄清:
当程序启动时,在初始化原语之前,将成为全局内存的内存空间中有什么?我试图了解它是否已归零,或例如垃圾.
When a program starts up, what is in the memory space which will become global memory, prior to primitives being initialized? I'm trying to understand if it is zeroed out, or garbage for example.
情况是:是否可以设置单例引用 - 在初始化之前通过 instance()
调用:
The situation is: can a singleton reference be set - via an instance()
call, prior to its initialization:
MySingleton* MySingleton::_instance = NULL;
结果得到两个单例实例?
and get two singleton instances as a result?
查看我对多个单例实例的 C++ 测验...
See my C++ quiz on on multiple instances of a singleton...
推荐答案
是的,全局原语被初始化为 NULL.
Yes global primitives are initialized to NULL.
示例:
int x;
int main(int argc, char**argv)
{
assert(x == 0);
int y;
//assert(y == 0); <-- wrong can't assume this.
}
你不能对堆上的类、结构、数组、内存块做任何假设......
You cannot make any assumptions about classes, structs, arrays, blocks of memory on the heap...
始终初始化所有内容是最安全的.
It's safest just to always initialize everything.
这篇关于全局内存是否在 C++ 中初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:全局内存是否在 C++ 中初始化?


- 使用/clr 时出现 LNK2022 错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- Stroustrup 的 Simple_window.h 2022-01-01
- C++ 协变模板 2021-01-01