全局内存是否在 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++ 中初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!