Asked  7 Months ago    Answers:  5   Viewed   572 times

Why do I receive the error "Variable-sized object may not be initialized" with the following code?

int boardAux[length][length] = {{0}};



I am assuming that you are using a C99 compiler (with support for dynamically sized arrays). The problem in your code is that at the time when the compilers sees your variable declaration it cannot know how many elements there are in the array (I am also assuming here, from the compiler error that length is not a compile time constant).

You must manually initialize that array:

int boardAux[length][length];
memset( boardAux, 0, length*length*sizeof(int) );
Tuesday, June 1, 2021
answered 7 Months ago

Yes your reasoning is correct, that is how these different forms of array declarations and definitions are viewed by C and C++.

As others already stated, VLA with a veritable variable length (non-const) in global scope is difficult to make sense. What would the evaluation order be, e.g if the the length expression would refer to an object of a different compilation unit? C++ doesn't have VLA, but it has dynamic initialization of objects at file scope. And already this gives you quite a head ache, if you have to rely on evaluation order.

This leaves the small gap for C concerning length expressions that contain a const qualified object, which isn't allowed. This comes from the fact that such objects are not considered "integer constant expressions" by the C standard. This could perhaps change in future versions, but up to now the C committee didn't find it necessary to allow for such a thing: there are enum constants that play that role in C. Their only limitation is that they are limited to int in C, it would be nice to also have them size_t.

Friday, June 18, 2021
answered 6 Months ago

Your two arrays are variable lenght arrays. You cannot initialize a variable length array in C.

To set all the int elements of your arrays to 0 you can use the memset function:

memset(plansza, 0, sizeof plansza);

By the way to initialize an array which is not a variable length array, the valid form to initialize all the elements to 0 is:

int array[31][14] = {{0}};  // you need the {}
Wednesday, August 4, 2021
Desmond Hume
answered 4 Months ago

First of all, n3639 was looking to put in place Arrays with Runtime Bound (ARB) not Variable Length Arrays (VLA). ARBs would support a subset of VLAs which excluded:

  • multidimensional arrays, where other than the top level has a runtime bound (in analogy, array-new doesn't support that either)
  • modifications to the function declarator syntax
  • sizeof(a) being a runtime-evaluated expression returning the size of a
  • typedef int a[n]; evaluating n and passing that through the typedef

In February of 2014 in Issaquah, Washington, at the standard committee unanimously voted to form the Array Extensions Technical Specification from n3820, it's initial revision originated from n3639 and the proposal of Dynarrays.

In May 2014 n4043 and n4050 attempted to address some "semi-editorial issues" in the Dynarray and ARB sections of the Array Extension Technical Specification, respectively.

But the standard committee's October 24 2014 teleconference cited huge disagreement on the language facilities, implementation possibilities, and desire for Array Extensions Technical Specification, ultimately describing it as in a state of limbo.

The standard committee's May 2015 meeting in Lenexa, Kansas went on to give the directional guidance that the Array Extensions Technical Specification would not be accepted in it's current form, and recommended:

Stripping the TS of its current contents, and waiting for a workable proposal to come along[1]

Ultimately the standard committee's March 2016 meeting in Jacksonville, Florida moved to close the Array Extensions Technical Specification at the confirmation that some array-related proposals are targeting the Library Fundamentals Technical Specification instead. There was a unanimous vote to do so with 8 strongly in favor, 5 in favor, and 6 abstaining.

Incidentally the only array related work going into the Library Fundamentals Technical Specification is the allowance of run-time creation of an array via make_array. Bjarne Stroustrup, the creator of C++, waxed eloquent on the topic:

We need arrays with run-time-specified bounds and safer access to such storage “yesterday”

Sadly, for Dr. Stroustrup, us, and the C++ community as a whole, there are no future plans to resurrect ARBs/VLAs with C++ in the simple c99 VLA form.

Wednesday, August 4, 2021
answered 4 Months ago

Isn't the value of N known at compile time?

No. At the time aMemberFunction is compiled, the compiler does not now what N is, since its value is determined at run-time. It is not smart enough to see that there is only one constructor, and assumes that the value of N could be different than 5.

Tuesday, August 24, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :