14

If we write something like:

int *arr = new int[5];

In this case, the system dynamically allocates space for 5 elements of type int and returns a pointer to the first element of the sequence.

But, once I saw the following code:

int *arr = new int[5]{};

So, What does mean {} after new operator? What is the purpose of {} in this code?

I have initialized array with my own value, like this:

#include <iostream>

int main()
{
    int* a = new int[5]{1};
    for(int i = 0; i < 5; i++)
        std::cout<< a[i]<<' ';

    delete[] a;
}

Output:

1 0 0 0 0

Only first element print 1. Why?

Jayesh
  • 4,303
  • 4
  • 25
  • 56
  • 8
    I recommend you take your time to read [this `new` reference](http://en.cppreference.com/w/cpp/language/new). In the table describing the syntax, note the *initializer* part. Then scroll down to the section about [Construction](http://en.cppreference.com/w/cpp/language/new#Construction) to read more about it. – Some programmer dude Oct 13 '17 at 06:44
  • 1
    This syntax has been in use since c++ 11. It allows to initialize an array with values specified inside the curly braces or in this case with zeros. – dsp_user Oct 13 '17 at 06:57
  • 5
    Use std::vector! –  Oct 13 '17 at 07:02
  • Same as https://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new but with C++11 syntax. – juanchopanza Oct 13 '17 at 09:04

2 Answers2

22
int *arr = new int[5];

performs a default initialization (which in this case means the elements of arr are uninitialized), while

int *arr = new int[5]{};

is a value initialization (which in this case means the elements of arr are zero-initialized). See the corresponding rules in the links. Specifically for the latter case, the array will be zero-initialized.

Anyway, if there is no other good reason, the use of std::vector should be preferred.

Edit: Concerning your second question: This is due to the initialization rules. You would have to write

int* a = new int[5]{1, 1, 1, 1, 1 };

The already recommended std::vector supports the syntax you desire:

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> a(5, 1);
    for(int i = 0; i < 5; i++)
        std::cout<< a[i]<<' ';
}

https://ideone.com/yhgyJg Note however the use of ()-braces here. As vector has an constructor for std::initializer_list, calling vector{ 5, 1} would impose a certain ambiguity, which is resolved by always preferring the initializer_list constructor for {}-initializations if provided. So using { } would cause to create a vector with 2 elements of values 5 and 1, rather than 5 elements of value 1.

Nawaz
  • 327,095
  • 105
  • 629
  • 812
Jodocus
  • 6,404
  • 1
  • 22
  • 38
  • Can I initialize an array with my own value? – Jayesh Oct 13 '17 at 06:59
  • Yes. Use e.g. list-initialization for individual array values: http://en.cppreference.com/w/cpp/language/list_initialization Edit: For the language lawyers: This is actually aggregate initialization as arrays are aggregates. – Jodocus Oct 13 '17 at 07:01
  • @Jayesh: Yes. Any expression, whether simply numbers, named constants, variables, mathematical expressions or function calls is allowed in that list. – MSalters Oct 13 '17 at 07:20
0

If you can tell the difference between

int a[5];

and

int a[5] = {0};

, the latter of which is a common practice to initialize a local array to all zero, you can also port the same syntax to new.

Aside from the std::vector solution, the most straightforward (and common) non-STL solution is iterating through the array for once:

for (int i = 0; i < sizeof(a)/sizeof(int); i ++)
    a[i] = 1;

FYI, using list initializing causes all unspecified array elements to be set to zero, e.g.

int a[5] = {[1] = 3};
// Now a is {0, 3, 0, 0, 0}
iBug
  • 30,581
  • 7
  • 64
  • 105