Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring array size for dynamically allocated array with initializer list

I'm trying to initialize a dynamically declared array with an initializer list but I noticed that I have to provide the array size with GCC or I get an error. Trying the same using MSVC does not cause any errors if the array size is left out. Is providing array size when using an initializer list with dynamic arrays mandatory or not? Is this something implementation-defined which is why it's different for both the compilers?

int *array { new int [3] {0, 1, 2} }; // Works with both MSVC and GCC.
int *array { new int [] {0, 1, 2} }; // Works only with MSVC, not GCC.
like image 418
Mohammad Ali Avatar asked Mar 24 '21 12:03

Mohammad Ali


People also ask

How do you declare an array of dynamic sizes?

In C you can use this method. int i=0; int *p; char c; int size; printf("Enter size :"); scanf("%d",&size); int *p=malloc(sizeof(int)*size); do { printf("Enter Number : "); scanf("%d",&p[i]); i++; printf("Press 'q' or 'Q' to quit or any other key to continue : "); scanf("%c",&c); } while(c!=

Can I provide array size dynamically?

A dynamic array is an array with a big improvement: automatic resizing. One limitation of arrays is that they're fixed size, meaning you need to specify the number of elements your array will hold ahead of time. A dynamic array expands as you add more elements. So you don't need to determine the size ahead of time.

Can I increase the size of dynamically allocated array ?*?

Simple answer is no, this cannot be done. Hence the name "static". Now, lots of languages have things that look like statically allocated arrays but are actually statically allocated references to a dynamically allocated array. Those you could resize.

How do you allocate the size of an array?

To allocate storage for an array, just multiply the size of each array element by the array dimension. For example: pw = malloc(10 * sizeof(widget));

How to initialize a dynamically allocated array to 0?

Initializing dynamically allocated arrays. It's easy to initialize a dynamic array to 0. Syntax: int *array{ new int[length]{} }; In the above syntax, the length denotes the number of elements to be added to the array. Since we need to initialize the array to 0, this should be left empty. We can initialize a dynamic array using an initializer ...

Is it possible to initialize a dynamic array using initializer lists?

Super annoying! However, starting with C++11, it’s now possible to initialize dynamic arrays using initializer lists! Note that this syntax has no operator= between the array length and the initializer list. For consistency, fixed arrays can also be initialized using uniform initialization:

How do you initialize an array to 0 in C++?

Initializing dynamically allocated arrays. If you want to initialize a dynamically allocated array to 0, the syntax is quite simple: 1. int *array{ new int[length]{} }; Prior to C++11, there was no easy way to initialize a dynamic array to a non-zero value (initializer lists only worked for fixed arrays).

What is the size of a dynamic array in Java?

Now, the underlying array has a length of five. Therefore, the length of the dynamic array size is 5 and its capacity is 10. The dynamic array keeps track of the endpoint. Features of Dynamic Array


Video Answer


1 Answers

This is P1009R2: Array size deduction in new-expressions, which was implemented for C++20.

Bjarne Stroustrup pointed out the following inconsistency in the C++ language:

double a[]{1,2,3}; // this declaration is OK, ...
double* p = new double[]{1,2,3}; // ...but this one is ill-formed!

Jens Maurer provided the explanation why it doesn’t work: For a new-expression, the expression inside the square brackets is currently mandatory according to the C++ grammar. When uniform initialization was introduced for C++11, the rule about deducing the size of the array from the number of initializers was never extended to the new-expression case. Presumably this was simply overlooked. There is no fundamental reason why we cannot make this work [...]

Proposed wording

The reported issue is intended as a defect report with the proposed resolution as follows. The effect of the wording changes should be applied in implementations of all previous versions of C++ where they apply. [...]

From GCC's C++ Standards Support pages we may note that GCC lists P1009R2 as implemented as of GCC 11, and we may verify that GCC 11 have back-ported the implemented to accept the OP's example as well-formed as far back as C++11.

DEMO (GCC 11 / -std=c++11).

like image 165
dfrib Avatar answered Oct 13 '22 03:10

dfrib