Dynamic arrays initialization

Dec 23, 2012 at 11:17pm
Pick and discuss which is the sanest method.

1
2
3
4
5
6
7
int *i1 = new int[20];
int *i2(new int[20]);
int *i3{new int[20]};

int *i4 = new int[20]();
int *i5(new int[20]());
int *i6{new int[20]()};

Dec 23, 2012 at 11:30pm
I would write
std::unique_ptr<int[]> i1(new int[20]); and
std::unique_ptr<int[]> i2(new int[20]());respectively (they do different things you know), since I'm not yet used to curlies everywhere.
Dec 23, 2012 at 11:49pm
they do different things you know

The first group behaves like malloc(), and the second like calloc(), filling the allocated memory with zeros, from what I remember. Correct?
Dec 24, 2012 at 2:07am
Yes, that's about right
Topic archived. No new replies allowed.