全局内存是否在 C++ 中初始化?

Is global memory initialized in C++?(全局内存是否在 C++ 中初始化?)
本文介绍了全局内存是否在 C++ 中初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)