7

I have see some example like below in a different question which I have not seen before.

new int[m_size]();
               ^^

I have seen and used the version new int[m_size] all the time but not one with the () at the end.

dubnde
  • 4,359
  • 9
  • 43
  • 63

2 Answers2

12

Two words : Value Initialization

new int[m_size](); array elements would be zero-initialized by writing () because () implies value initialization.1 (zero initialization for a primitive type)

1: An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized. ( $8.5/7 )

Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • ah! So which type does it value initialise? Is it the pointer type of the whole expression or the individual members of the array? – dubnde Apr 05 '11 at 12:37
  • 1
    @MeThinks : Check out `8.5/5` (Value Initialization). For a primitive type **Value initialization** => **Zero Initialization**. In this case the all the elements of the array would be initialized to `0` – Prasoon Saurav Apr 05 '11 at 12:39
  • ok. Got it in the standard "if T is an array type, then each element is value-initialized". Thanks for the prompt response. First time I've come across it with respect to arrays. – dubnde Apr 05 '11 at 12:41
2

it means all the elements will be zero initialized,similar to calloc(o,sizeof(int)) where with this calloc ,ur initializing a single integer on heap with 0

Vijay
  • 65,327
  • 90
  • 227
  • 319