Does using new[]
in C++ initialise the allocated array to all zeroes?
Asked
Active
Viewed 200 times
0

kinokijuf
- 968
- 1
- 11
- 33
-
No you have to do it yourself – alexbuisson Jan 17 '14 at 11:00
1 Answers
7
Does new[] initialise an array of builtins?
It depends:
int* = new int[42]; // default initialization: elements not initialized
int* = new int[42](); // value initialization: elements are zero initialized
Note the terminology: in the first example, the elements are said to be default-initialized, which for built-ins means no initialization is performed. In the second example, the elements are value-initialized, which for built-ins means zero-initialization.

juanchopanza
- 223,364
- 34
- 402
- 480