Why can't I create an array of size n?

49,984

Solution 1

int n;
cin >> n;
int array[n];

This will work if use g++. g++ support VLAs as an extension. However ISO C++ mandates size of an array to be a constant expression i.e the size must be known at compile time.

Why is it that C++ don't allow you to create array of dynamic length in stack memory?

Simple answer "Because the standard says so". Even the upcoming C++ Standard (C++0x) is not going to allow Variable Length Arrays.

BTW we always have std::vector in C++. So there's no reason to worry. :)

Solution 2

C99 does allow variable length arrays (VLAs); C89 did not.

void function(int n)
{
    int array[n];
    ...
}

C++ (98, 03) does not allow VLAs in the same way that C99 does, but it has vectors and related types which are better in many respects.

Solution 3

There is no language called C/C++. There is C, which allows variable-length arrays (VLAs) since 1999, and there is C++, which doesn't allow.

To answer your question "why" - C++ standard body for whatever reason didn't include VLAs into C++ standard.

Solution 4

int n;
cin >> n;
int array[n];

In C++, the size of the array must be known at compile time. But in your code the size will be known at runtime. That is not allowed by the language!

Share:
49,984
Mihran Hovsepyan
Author by

Mihran Hovsepyan

Software Engineer at OneMarketData

Updated on August 17, 2022

Comments

  • Mihran Hovsepyan
    Mihran Hovsepyan almost 2 years

    Possible Duplicate:
    Why can't I create an array with size determined by a global variable?

    This is definition of simple array with constant size 4, which is stored in stack memory:

    int array[4];
    

    Now If I want to declare array of dynamic size in stack it seems that I should write this code:

    int n;
    cin >> n;
    int array[n];
    

    But as we know this is not allowed in C++ and instead we can write this one, which will create the array in dynamic memory (i.e. heap):

    int n;
    cin >> n;
    int *array = new int[n];
    

    But this is more slower and (because of using new operator) and requires to call delete [] operator after we finish our work with array.

    So my question is here:

    • Why is it that C++ don't allow you to create array of dynamic length in stack memory?
  • Mihran Hovsepyan
    Mihran Hovsepyan about 13 years
    I know this. But the question is "why?"
  • Jonathan Leffler
    Jonathan Leffler about 13 years
    @Mihran: because the designers of the original C did not see it as necessary, and C++ inherited that limitation from C. Then C was adjusted in C99 to allow it - and C++ has not yet followed suit, and AFAIK, even the C++0x standard won't add the functionality. Not all C compilers (and notably not MSVC) support C99 to the extent of allowing VLAs. The original reason for omitting the support was probably that providing support for VLAs complicated compilers that had to be shoe-horned to fit into 64 KB memory, and insufficient need for it; dynamically allocated arrays work well for many people.
  • Mihran Hovsepyan
    Mihran Hovsepyan about 13 years
    The qestion is "Why there is such requairment?"
  • Mihran Hovsepyan
    Mihran Hovsepyan about 13 years
    +1 I did not know about this.
  • Falcon Momot
    Falcon Momot almost 10 years
    When you know n in advance, it is very often more efficient to use an array, but you have to create it like a pointer and use malloc() or alloca(). You can use pointers like arrays.