如何使 `new[]` 默认初始化原始类型数组? [英] How can I make `new[]` default-initialize the array of primitive types?

查看:25
本文介绍了如何使 `new[]` 默认初始化原始类型数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不时需要为内置类型(通常是 char)调用 new[].结果是一个带有未初始化值的数组,我必须使用 memset()std::fill() 来初始化元素.

Every now and then I need to call new[] for built-in types (usually char). The result is an array with uninitialized values and I have to use memset() or std::fill() to initialize the elements.

如何让 new[] 默认初始化元素?

How do I make new[] default-initialize the elements?

推荐答案

int* p = new int[10]() 应该可以.

然而,作为 Michael 指出,使用 std::vector 会更好.

However, as Michael points out, using std::vector would be better.

这篇关于如何使 `new[]` 默认初始化原始类型数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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