Topic: Variable sized arrays


Author: alexis@maui.cs.ucla.edu (Alexis Wieland)
Date: Sun, 09 Jan 94 21:06:54 GMT
Raw View
Apologies if this has been beaten to death in the past, but:

For years now I have been using variable sized arrays in g++,
that is something like:

void foo (int n) {
    int a[n];
    // do something with 'a'
}

I just started to use CC and am horrified to find this isn't allowed
!?  I checked Stroustrup and sure enough array bounds are supposed to
be constants.  This must be a g++ extension to c++.  This seems
foolish.  I mean clearly this is the same as

void foo (int n) {
    int *a = new int[n];
    // do something with 'a'
    delete [] a;
}

so it's not a language extension as much as a shorthand.
but the later version is (a) longer, and (b) more error prone.

Is there a reason the (IMHO) very useful and desirable feature is not
included in the language definition?

Please reply to me directly, I'm only a intermittent reader of comp.std.c++

Thanks in advance,

   - alexis.                          <alexis@cs.ucla.edu>