简体   繁体   English

C ++中动态分配内存的初始值

[英]Initial Value of Dynamically-Allocated Memory in C++

在C ++中,当您动态分配的阵列int ,是的值int小号未定义或初始化为0?

    int *array = new int[50];

The term is uninitialized . 这个词是未初始化的 But it depends how you initialize the array. 但这取决于你如何初始化数组。 You could value-initialize it : 你可以初始化它

int *array = new int[50]();

and the values would be 0. 并且值将为0。

If you leave it uninitialized, you can't know what values are there because reading from them would be undefined behavior. 如果你保持未初始化状态,你就无法知道它们的值是什么,因为从中读取它们将是未定义的行为。

If you use vectors instead of arrays, you will get an initial value of 0 for all elements: 如果使用向量而不是数组,则所有元素的初始值均为0:

std::vector<int> v(50);

If you want a different default value, you can specify one: 如果需要不同的默认值,可以指定一个:

std::vector<int> v(50, 42);

An additional benefit of vectors is that you don't have to manually release the underlying array. 向量的另一个好处是您不必手动释放底层数组。

To answer your question no.. But there is a way to set default values. 要回答你的问题,但是有一种设置默认值的方法。 Try: 尝试:

int *arr = new int[SIZE]() ;

The above is C++ standard but may not work on all compilers. 以上是C ++标准,但可能不适用于所有编译器。 The safe bet would be to use a loop and initialize it to a default value of your choice. 安全的赌注是使用循环并将其初始化为您选择的默认值。

for(int i=0; i < SIZE; i++)
{
  arr[i] = 1; //1 being my default value
}

EDIT: As others have pointed out even better: memset 编辑:正如其他人指出的更好: memset

They will be undefined. 它们将是未定义的。 Just garbage depending on what was in those locations before you initialized it 在初始化之前,根据这些位置中的内容进行垃圾处理

If you write 如果你写

int *array = new int[50];

then the values in the array can contain anything, but if you write 那么数组中的值可以包含任何内容,但是如果你写的话

int *array = new int[50]();

then you will be calling the "default constructor" and everything will be 0. 那么你将调用“默认构造函数”,一切都将为0。

它们更新,它们包含分配之前的任何数据。

In C/C++, allocate an object in memory means simply reserving a number of memory blocks to that object. 在C / C ++中,在内存中分配对象意味着只需为该对象保留大量内存块。 The initial value is basically what was present in those blocks which is most of the time random values. 初始值基本上是那些块中存在的大部分时间随机值。

You need to assume it is garbage, even though it may often (especially in debug builds) be initialised to zero or some other predefined value indicating uninitialised memory (usually some HexSpeak value if zero is not used). 您需要假设它是垃圾,即使它可能经常(特别是在调试版本中)初始化为零或某个其他预定义值指示未初始化的内存(通常一些HexSpeak值,如果不使用零)。

Eg see http://www.nobugs.org/developer/win32/debug_crt_heap.html#table 例如,请参阅http://www.nobugs.org/developer/win32/debug_crt_heap.html#table

It is completely undefined by the standard, and really just depends on your OS and compiler. 它完全没有被标准定义,实际上只取决于你的操作系统和编译器。 In some compilers, it just uses whatever the OS gave you, and in some OSes that will be 0s, others garbage, and others something else. 在一些编译器中,它只使用操作系统给你的任何东西,在一些操作系统中将为0,其他操作系统为垃圾,其他操作系统为其他操作系统。 Or the compiler could automatically insert an invisible memset after a malloc or new . 或者编译器可以在mallocnew之后自动插入一个不可见的memset But in any case, the point is, never rely on the value. 但无论如何,重点是,永远不要依赖价值。 Even if your current version of your compiler makes it 0s, say, that might change in a future version, and probably won't be true on another compiler. 即使你的编译器的当前版本使其为0,比如说,在将来的版本中可能会改变,而在另一个编译器中可能不会这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM