0

I thought that I understood what a VLA is, until I saw a question asked here about the difference between dynamic memory allocation vs variable length arrays. So I don't have any problem, at least for now, with dynamic memory allocations, but what I don't understand is why this code is considered a VLA:

int size = 10; // or whatever
int my_array [size] ; // why this is a VLA

The more mysterious thing is that this is even supposed to be a VLA

const int size = 10;
int my_array [size];  // why even when i use a const it is a VLA .

So, my question is, how are these 2 different methods considered as VLA?

As far as I know, a VLA is just an array that its size is only know at runtime, like if I prompt the user to enter the size of the array and set the array to this size, so that there is no way the compiler will ever know the size of the array.

But, in both pieces of code above, the size is already known at compile time, so if that is true that these are VLA, are standard array syntax must be like that and nothing else.

int my_array[10];

My other question is, that I heard that const int size =10; is not actually a const, which means the compiler doesn't know if this size is 10, it treats it as a variable.

So, if anyone can clear things up a little bit about the difference between a variable and const in C++, that would be really appreciated.

NOTE: this is the link to the StackOverflow question. So, if there is something wrong in this question, someone can correct thing up.

What's the difference between a VLA and dynamic memory allocation via malloc?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
KMG
  • 1,433
  • 1
  • 8
  • 19
  • 6
    C and C++ are different languages. In C `const int size = 10; int my_array [size];` is VLA, in C++ isn't. You linked a question about C, yet you tagged C++ - are you interested in C++ or C? – KamilCuk May 27 '20 at 04:32
  • 1
    You are right that an optimizing compiler *could* produce identical code for both versions. But because one could, in the future, add e.g. a scanf() to change the value between the definition and use of the non-const size the compiler likely already prepares for that and produces code which actually retrieves the value from memory, even though it can see that right now no such thing is happening. This is not the case for the const version: The value cannot (legally) change. – Peter - Reinstate Monica May 27 '20 at 04:36
  • i am interested in c++ , but that doesn't clarify why this is even a VLA size is still known to the compiler so why my_array is still a VLA and how c and c++ differ in the way they use const keyword – KMG May 27 '20 at 04:38
  • The compiler *could* deduce the value at compile time, but the C++ standard says that the int here isn't constant unless you explicitly make it constant so it's a VLA. In general stuff in C++ isn't considered constant by default whether the value can be deduced at compile time or not. – eesiraed May 27 '20 at 04:50
  • `int size = 10;` is not a compile-time constant. It is a variable allocated at runtime and then initialized with a constant value. Thus `my_array` in that example is a VLA. `const int size = 10;` is a compile-time constant, so much that the compiler is allowed to optimize it away and use the *value* directly everywhere the *name* is used. Thus `my_array` in that example is not a VLA. – Remy Lebeau May 27 '20 at 04:57
  • Playing around with the compiler explorer (https://www.godbolt.org/z/Ecy8YC) shows that the non-const array produces a lot of instructions (no idea what they are doing) that disappear with optimization (change the command line options in the edit field on top, e.g. to "-O0" for no optimization). Both clang and gcc do that. So yes, the compiler sees that it can ignore that it's a VLA. – Peter - Reinstate Monica May 27 '20 at 04:57
  • 1
    `i am interested in c++` then VLA doesn't exists, there is no VLA in C++. – KamilCuk May 27 '20 at 05:01

1 Answers1

2

First of all, variable length arrays doesn't exists in C++. There are no variable length arrays in C++. This answer only applies to specific GNU compiler extension to support variable length arrays in C++.

why this is even a VLA size is still known to the compiler

The question is not about "what is known to the compiler", the question is about the definitions. The terms and language are defined and it is defined what is a VLA and what isn't. A variable length array is defined as an array that size is not a integer constant expression - int vla[<not integer constant expresion>];. From C99 6.7.5.2p4:

If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

What is and isn't an integer constant expression has in turn once again a very certain definition.

In C, with both definitions int size = 10; and const int size = 10; the size as an expression is not an integer constant expression. So both array definitions you showed are variable length arrays. The end.

In C++, there is no integer constant expression and most probably (my guess!) GNU compiler working in C++ with VLA extension enabled checks if the size expression inside array declaration is usable in a constant expression. In C++, a const-qualifed integral type is usable in a constant expression, so const int size = 10; int my_array [size]; is a normal array. An integral type that isn't const-qualifed is not usable in a constant expression, so int size = 10; int my_array [size]; is invalid in C++ and in a GNU compiler with extensions enabled results in a variable length array.

Why is this code considered a VLA even though it uses an int?

"Using an int" doesn't necessarily make an expression a constant expression, which is needed for expression inside brackets inside an array definition that isn't a variable length array.

how are these 2 different methods considered as VLA?

As for C, the expression inside brackets inside an array definition has to be a constant expression, because it isn't, these two code snippets result in a VLA declaration.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111