Open In App

Why Variable Length Array were Removed in C++?

Last Updated : 11 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ Program to Implement Dynamic Array of Variable Length
#include <iostream>
using namespace std;
  
int main()
{
  
    // Declare a variable to store size of the array
    int size = 5;
  
    // Allocate memory for int type array in heap
    // Store the address of first element in the pointer
    int* variableLengthArray = new int[size];
  
    // Initialize value to the array
    for (int i = 0; i < size; i++) {
        variableLengthArray[i] = i+1;
    }
  
    // Print the array
    cout << "The elements in the array: ";
    for (int i = 0; i < size; i++) {
        cout << variableLengthArray[i] << " ";
    }
  
    // free the memory after usage
    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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads