C ++中动态分配内存的初始值 [英] Initial Value of Dynamically-Allocated Memory in C++

查看:562
本文介绍了C ++中动态分配内存的初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,当您动态分配 int 的数组时,是 int 未定义的值初始化为0?

In C++, when you dynamically allocate an array of int, are the values of the ints undefined or initialized to 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]();

,值为0。

如果您将其初始化,您将无法知道有什么值,因为从它们的阅读将是未定义的行为。

If you leave it uninitialized, you can't know what values are there because reading from them would be undefined behavior.

这篇关于C ++中动态分配内存的初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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