GFact | Why doesn’t C++ have Variable Length Arrays library?
While studying DSA, we all have encountered Arrays and their fixed-size property. But have you ever thought about Why C language allows us the Variable-Length Array but C++ doesn’t? And what should we do in C++ if we want a Variable-Length Array?
Well, we will talk about the above in detail in the following post but first, we should know about the term Variable-length Array.
What does a Variable-Length Array mean?
Variable-Length Array term is used for runtime sized or variable sized arrays. The size of such arrays is defined at run-time.
Variably modified types must be declared at either block scope or function prototype scope and include:
- variable-length arrays and
- pointers to variable-length arrays.
Variable-Length Array is a feature where we can allocate an auto array (on stack) of variable size. It can be used in a typedef statement.
Example to show Variable-Length Array
Below program compiles and runs fine in C language:
#include <stdio.h> void fun(int n) { // Creating a Variable-Length Array // based on the size passed as Input to this function int arr[n]; // ...... } int main() { // Passing a size for creation of // Variable Length Array at runtime fun(6); }
Does C or C++ Support Variable-Length Array?
C Language supports Variable-Length Array from C99 standard.
Note: In C99 or C11 standards, there is feature called flexible array members, which works same as the above.
C++ standard ( till C++11 ) doesn’t support Variable-Length Array. The C++11 standard mentions array size as a constant expression. So the above program may not be a valid C++ program.
The program may work in GCC compiler, because GCC compiler provides an extension to support them.
As a side note, the latest C++14 mentions array size as a simple expression (not a constant expression) and hence can be used for creating a Variable-Length Array.
Why doesn’t C++ support Variable-Length Array?
Variable-Length Array (VLAs) are not part of the C++ standard because they were not included in the original C++98 standard. The C++ language is based on C, and VLAs were not part of the C standard at the time. Additionally, the C++ standardization committee has chosen to focus on other features and improvements, such as templates and the Standard Template Library (STL), rather than adding VLAs to the language. Some C++ compilers support VLAs as an extension to the language, but they are not a standard feature.
What are the alternatives for Variable-Length Array in C++?
In C++ STL library, we have Vectors which works as a dynamic array which can be used in the place of Variable-Length Array, in which we can define the length of the vector on the runtime itself .
Below is the code for vectors in C++ :
C++
#include <iostream> #include <vector> using namespace std; int main() { int n = 5; // Initialize the vector with variable size n vector< int > v(n); for ( int i = 0; i < n; i++) { v[i] = i; } // printing the v vector for ( int i = 0; i < n; i++) { cout << v[i] << " " ; } cout << "\n" ; // Initialize a vector without any size vector< int > vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5); // ... // To iterate over it: for ( auto it : vec) { cout << it << " " ; } cout << endl; return 0; } |
0 1 2 3 4 1 2 3 4 5