0

I just read a tutorial about dynamic memory in C++ and it states as follows:

...the size of a regular array needs to be a constant expression, and thus its size has to be determined at the moment of designing the program, before it is run...

However, I just ran a program to test this:

#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
    int y;
    cout << "Enter number of elements of array: ";
    cin >> y;
    int x[y];   // I declared an array using a variable size instead of a constant expression
    x[y-1] = 3;
    cout << x[y-1];

    return 0;
}

...and there were no errors. So is the statement made by the tutorial incorrect or am I misinterpreting it?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
rasnauf
  • 353
  • 3
  • 9

4 Answers4

3

No, they're not.

What you're seeing here is a GNU extension called "variable length arrays" (which are still stack-based).

The quotation is correct in the context of the actual C++ language itself.

If you used proper compilation flags (-Wall -Wextra -pedantic) then your compiler would tell you this.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    Your answer is incorrect. Arrays may be dynamically allocated. Also there is no such term as "actual C++". I down voted your answer. – Vlad from Moscow Jun 14 '15 at 19:14
  • 7
    Haha nice try @Vlad. I never said arrays _cannot_ be dynamically allocated; I answered the actual question, which was about the validity of the quote "the size of a regular array needs to be a constant expression". As for "actual C++", well, I don't know what drugs you're on, but, yes, there is "actual C++". That means the C++ language as defined by the ISO C++ working group. I do appreciate that you've now made an effort to make your downvote less about "revenge" and more about factual inaccuracies, even though your supposed factual inaccuracies do not actually exist here. – Lightness Races in Orbit Jun 14 '15 at 19:16
  • 1
    Between "actual C++" and the ISO C++ there is a big gap. "Actual C++" is the C++ that the author of the topic used. Moreover taking into account your comments to my post you even do not know the notion of constant expression. You do not know what grammars of artificial languages exist. – Vlad from Moscow Jun 14 '15 at 19:24
  • 8
    @VladfromMoscow: I have no idea what you're talking about, and neither do you. And I know full well what a constant expression is, pally. It has very little to do with grammar. – Lightness Races in Orbit Jun 14 '15 at 19:25
  • 1
    Of course you have no idea because you simply do not know the definition of the array declarator in C++ noptr-new-declarator [ constant-expression ] attribute-specifier-seqopt and in C direct-declarator [ type-qualifier-listopt assignment-expressionopt ] Do you see difference , son? – Vlad from Moscow Jun 14 '15 at 19:27
  • 7
    It's time I left you to your drug-induced fantasies as there is obviously no talking sense in to you today. Nor indeed, if memory serves, any other day. "Unemployed" indeed. I wonder why that may be...? – Lightness Races in Orbit Jun 14 '15 at 19:28
2

Your program isn't legal C++. The compiler you are using has an extension to allow you to use a non-constant expression as an array size. Here's what my compiler has to say about it:

main.cpp:10:10: warning: variable length arrays are a C99 feature [-Wvla-extension]
    int x[y];   // I declared an array using a variable size instead of a constant expression
         ^

Furthermore, this has nothing to do with dynamic allocations: your array is still stored on the stack just like any other automatically allocated object.

bames53
  • 86,085
  • 15
  • 179
  • 244
0

As others have already stated your compiler is using an extension for this. When declare array like this ->

int arr[100];

C++ allocates it on stack. To create dynamic array in c++, you have to declare it in following way:

int *arr;
arr = new int[n];

Dynamic arrays are allocated using heap.

cham3333
  • 103
  • 1
  • 2
  • 8
-3

You used a compiler that has its own language extension that does not C++ compliant. C++ does not allow Variable Length Arrays as in your program However C does allow to define such arrays. So some C++ compilers include this feature of the C Standard in their C++ grammar.

In C++ the declarator of an array is defined the following way

noptr-declarator [ constant-expressionopt] attribute-specifier-seqopt

while in C it is defined according to the following grammar rule

direct-declarator [ type-qualifier-listopt assignment-expressionopt ]

So the language extension of your compiler uses the definition of the array declarator similar to the C definition of the array declarator.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335