是全局内存在C ++初始化吗? [英] Is global memory initialized in C++?

查看:90
本文介绍了是全局内存在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;

并获得两个单例实例?

请参阅我的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 ++初始化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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