Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable length arrays (VLA) in C and C++

I have some concepts about the VLA and its behavior that I need to clarify.

AFIK since C99 it's possible to declare VLA into local scopes:

int main(int argc, char **argv)
{
    // function 'main' scope
    int size = 100;
    int array[size];
    return 0;
}

But it is forbidden in global scopes:

const int global_size = 100;
int global_array[global_size]; // forbidden in C99, allowed in C++

int main(int argc, char **argv)
{
    int local_size = 100;
    int local_array[local_size];
    return 0;
}

The code above declares a VLA in C99 because the const modifier doesn't creates a compile-time value. In C++ global_size is a compile-time value so, global_array doesn't becomes a VLA.

What I need to know is: Is my reasoning correct? The behaviour that I've described is correct?

I also want to know: Why the VLA in global scope aren't allowed? are forbidden both in C and C++? What reason is there for the behavior of arrays into global and local scope were different?

like image 748
PaperBirdMaster Avatar asked Dec 28 '12 20:12

PaperBirdMaster


People also ask

What are variable length arrays in C?

A variable length array, which is a C99 feature, is an array of automatic storage duration whose length is determined at run time.

Does C allow variable length arrays?

Variable length arrays is a feature where we can allocate an auto array (on stack) of variable size. It can be used in a typedef statement. C supports variable sized arrays from C99 standard.

Why are variable length arrays bad in C?

The biggest problem is that one can not even check for failure as they could with the slightly more verbose malloc'd memory. Assumptions in the size of an array could be broken two years after writing perfectly legal C using VLAs, leading to possibly very difficult to find issues in the code.

How do you create an array with variable length?

If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself: int n = 10; double* a = new double[n]; // Don't forget to delete [] a; when you're done!


1 Answers

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.

like image 84
Jens Gustedt Avatar answered Sep 18 '22 14:09

Jens Gustedt