Search code examples
c++arraysinitialization

How can I make `new[]` default-initialize the array of primitive types?


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.

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


Solution

  • int* p = new int[10]() should do.

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