Why Variable Length Array were Removed in C++?
Last Updated :
11 Dec, 2023
While historically a feature in the C language, the variable length array is not part of standard C++. This article discusses the C++’s approach to dynamic memory allocation, showing an alternative strategy employed to achieve a similar outcome.
Variable Length Array
The Array is said to be a variable length array if the size is defined at the runtime. The size of the array can be specified by the user or the number determined by some other condition. The memory can be allocated for the variable length array during execution
In C language, we can create a variable length array as it is a feature of C. However, C++ doesn’t support variable-length array features to enhance the performance in terms of avoiding the possibilities of undefined behavior.
In C language, the variable length array can be created with the following syntax easily.
data_type array_name[array_size];
Example: int arr[n];
In C++, the compiler must know the amount of memory to allocate at the compile time, but since the size of the variable is unknown before runtime, the above approach may produce errors as it is not supported.
Note: You may wonder that you have used the above declaration to declare array in C++. This is because some C++ versions may not cause error. GCC compiler provides an extension to supports variable length array. However, this feature is not supported by C++ standard.
Approach to Create Variable Length Array in C++
We can create variable-length arrays in C++ by using dynamic memory allocation during execution. This approach uses “new” keyword to allocate memory and “delete” keyword to release the memory.
So, what is dynamic memory allocation?
As opposed to allocating memory only at the beginning of compilation, dynamic memory allocation makes it possible as the program runs. This implies that the program can ask the system for memory when needed, depending on what the program needs at the time.
new operator is used to ask the system for memory, and delete operator is used to give back memory to the system that we no longer need. This comes handy when the program’s execution may require more memory than we have anticipated.
Syntax to Allocate Dynamic Array in C++
data_type* array_name = new data_type[size];
Syntax to Deallocate Dynamic Array in C++
delete[] array_name;
Note: It is recommended to deallocate memory after usage to avoid memory leakage and unwanted memory usage.
C++ Program to Implement Variable Length Array
C++
#include <iostream>
using namespace std;
int main()
{
int size = 5;
int * variableLengthArray = new int [size];
for ( int i = 0; i < size; i++) {
variableLengthArray[i] = i+1;
}
cout << "The elements in the array: " ;
for ( int i = 0; i < size; i++) {
cout << variableLengthArray[i] << " " ;
}
delete [] variableLengthArray;
return 0;
}
|
Output
The elements in the array: 1 2 3 4 5
Explanation
Here, memory is requested from the heap at runtime, making it possible to create an array whose size is known as the program runs.
Share your thoughts in the comments
Please Login to comment...