What is the purpose of "{}" in "new int[5]{};"?
Asked Answered
F

2

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?

Facia answered 13/10, 2017 at 6:41 Comment(4)
I recommend you take your time to read this new reference. In the table describing the syntax, note the initializer part. Then scroll down to the section about Construction to read more about it.Radiogram
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.Merrymerryandrew
Use std::vector!Aceldama
Same as #620637 but with C++11 syntax.Ellsworth
H
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.

Haehaecceity answered 13/10, 2017 at 6:48 Comment(3)
Can I initialize an array with my own value?Facia
Yes. Use e.g. list-initialization for individual array values: en.cppreference.com/w/cpp/language/list_initialization Edit: For the language lawyers: This is actually aggregate initialization as arrays are aggregates.Haehaecceity
@Jayesh: Yes. Any expression, whether simply numbers, named constants, variables, mathematical expressions or function calls is allowed in that list.Leuko
R
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}
Rivy answered 16/10, 2017 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.