如何使`new []`默认初始化原始类型数组?

2022-01-18 00:00:00 arrays initialization c++

我不时需要为内置类型(通常是 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.

相关文章