Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validity of int * array = new int [size]();

int * array = new int [size]();

The operator() allow to set all values of array to 0 (all bits to 0). it's called value-initialization.

Since which version of g++ is it valid?

What about other compilers?

Where can I find it in standard?

like image 915
Nadir SOUALEM Avatar asked Nov 17 '09 20:11

Nadir SOUALEM


People also ask

What is int a []= new int n ];?

int *array = new int[n]; It declares a pointer to a dynamic array of type int and size n . A little more detailed answer: new allocates memory of size equal to sizeof(int) * n bytes and return the memory which is stored by the variable array .

What is the difference between int array [] and int [] array?

There is no difference in these two types of array declaration. There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays.

What is the size of array defined below in bytes int a [] New int 100 ];?

The size of the integer is 4 bytes.

What is int * A in C++?

int means a variable whose datatype is integer. sizeof(int) returns the number of bytes used to store an integer. int* means a pointer to a variable whose datatype is integer. sizeof(int*) returns the number of bytes used to store a pointer.


1 Answers

This is part of the C++ standard; if it was invalid in g++ then g++ was nonconforming. From the C++ standard (ISO/IEC 14882:2003), several sections are relevant:

5.3.4/15 concerning the new expression says:

If the new-initializer is of the form (), the item is value-initialized

8.5/5 concerning initializers says:

To value-initialize an object of type T means:

— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;

— if T is an array type, then each element is value-initialized;

— otherwise, the object is zero-initialized

So, for an array of ints, which are a scalar type, the third and fourth bullet points apply.

like image 158
James McNellis Avatar answered Sep 28 '22 05:09

James McNellis