Does C++ Support Variable Length Arrays?



No, C++ does not support Variable Length Arrays (VLAs). A Variable length array is an array whose size is determined at runtime, not at compile time. These types of arrays are only supported in C99 version of C language. In this article, we will discuss the reasons why C++ does not support VLAs and what alternatives are available for variable array allocation.

What is Variable Length Array (VLA)?

A Variable Length Array (VLA) is an array whose size can be determined at runtime. It is not supported in standard C++. But, in C99 you can declare a VLA like this:

// Only valid in C99

void func(int n) {
    int arr[n];  
}

func(5);  // Call with size 5

In the above code, the size of the array arr is determined at runtime based on the value of n passed to the function func().

Why C++ Does Not Support VLAs?

C++ does not support VLAs for following reasons:

  • Proper Memory Management: C++ focuses on strong type safety and memory management. VLAs can cause issues with memory allocation and deallocation.
  • Undefined Behavior: VLAs can lead to undefined behavior if the size is not properly managed.
  • Have alternatives for VLAs: C++ provides std::vector, dynamic allocation with 'new' as an alternative to VLAs. Here, either C++ automatically manages memory or developer can manage memory manually using new and delete.

Alternatives to VLAs in C++

In C++, you can achieve similar functionality to VLAs using the following alternatives:

1. Using Vectors

The vectors in C++ are a type of dynamic array that can grow and shrink in size at runtime. They are part of the C++ Standard Template Library (STL) defined in <vector> header file. Vectors are more flexible than arrays, as they can change size dynamically and manage memory automatically. Following code snippet shows how to declare and use a vector in C++:

#include <iostream>
#include <vector>
using namespace std;

void func(int n) {
    vector<int> arr(n);  // Allocate vector of size n

    // Fill with example values
    for (int i = 0; i < n; ++i) {
        arr[i] = i * 2;
    }

    // Display the vector
    cout << "Contents of vector:\n";
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    func(5);  // Call with size 5
    return 0;
}

The output of the above code will be:

Contents of vector: 0 2 4 6 8 

2. Dynamic Allocation with new

In C++, you can also allocate memory dynamically using the new keyword. But in this method, you have to manually deallocate the memory using the delete[] operator to avoid memory leaks. Following code shows how to declare and use a dynamically allocated array using new in C++:

#include <iostream>
using namespace std;

void func(int n) {
    // Dynamically allocate array
    int* arr = new int[n];  

    // Fill with example values
    for (int i = 0; i < n; ++i) {
        arr[i] = i * 2;
    }

    // Display the array
    cout << "Contents of array:\n";
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;

    delete[] arr;  // Deallocate memory
}

int main() {
    func(5);  // Call with size 5
    return 0;
}

The output of the above code will be:

Contents of array: 0 2 4 6 8 

3. Using std::unique_ptr

The std::unique_ptr in C++ provides a safer way to manage dynamically allocated arrays. It automatically frees memory when it goes out of scope. So, not needed to manually call delete[] to free up the memory. It is part of the <memory> header file and is a modern C++ alternative to raw pointers. Following code snippet shows how to use std::unique_ptr with a dynamically allocated array:

#include <iostream>
#include <memory>
using namespace std;

void func(int n) {
    unique_ptr<int[]> arr(new int[n]);  // Safer dynamic allocation

    // Fill with example values
    for (int i = 0; i < n; ++i) {
        arr[i] = i * 2;
    }

    // Display the array
    cout << "Contents of array:\n";
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    func(5);  // Call with size 5
    return 0;
}

The output of the above code will be:

Contents of array: 0 2 4 6 8 
Farhan Muhamed
Farhan Muhamed

No Code Developer

Updated on: 2025-05-27T18:10:41+05:30

268 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements