1

First semester programming student here. For context, we are studying pointers in c++, and are given examples of how to use them in order to initialize arrays with a variable length. For example,

int length;
int *p;
cout<<"Please enter the length of the array: ";
cin>>length;
p = new int[length];

However, for experimentation, I wanted to test the following code to see what errors I get...

int main(){

    int length;
    cout << "Please enter the length of the array: " << endl;
    cin >> length;
    int array[length]; //standard array initialized after recieving input from user

    for(int i=0; i<length; i++){
        array[i] = rand()%100;
    }

    for(int i=0; i<length; i++){
        cout << array[i] << " ";
    }

    return 0;
}

My confusion comes from the fact that this code executed just fine. I can test with various input lengths and the array will fill and print just fine. Can anyone explain why this might be the case? Thank you for your time.

ATLayne
  • 11
  • 1
  • 5
    It should be because of compiler extensions. Try `-pedantic` option if you are using GCC. – MikeCAT Jul 25 '22 at 22:46
  • 3
    [Extra reading](https://stackoverflow.com/q/1887097/4581301) – user4581301 Jul 25 '22 at 22:48
  • 2
    Change `int array[length];` to `std::vector array(length);`. – Eljay Jul 25 '22 at 22:52
  • Alternatively: you can use `auto array = std::make_unique(length);` if you are not going to resize the array after creating it – Remy Lebeau Jul 25 '22 at 22:55
  • _"given examples of how to use them in order to initialize arrays with a variable length"_ - then in C++ there is one wonderful class that'll help. `std::vector`. But ... there are more! What do you need exactly? – Ted Lyngmo Jul 25 '22 at 22:56
  • 4
    I've always felt that g++ and clang should change their defaults to *not* use extensions. If they did that, then a huge chunk of StackOverflow questions would start off using correct C++ syntax, and a lot of bad C++ websites will need to change their wrong example code that they're feeding newbie programmers. – PaulMcKenzie Jul 25 '22 at 22:58
  • 1
    @PaulMcKenzie Pure speculation: clang++ only does so to not break compatibility with `g++`. (and I agree - harden the "default") – Ted Lyngmo Jul 25 '22 at 23:09
  • @MikeCAT Thank you. I just tried that with g++ and clang++. Both compile, but spit out a warning that reads "warning: variable length arrays are a C99 feature [-Wvla-extension]" – ATLayne Jul 26 '22 at 01:37
  • @PaulMcKenzie What makes extensions not the "standard". I was under the impression that whatever the latest extension is becomes the new "standard"? Just curious at this point. – ATLayne Jul 26 '22 at 02:22
  • @ATLayne You mix up two different things. There is the progress made from one standard to another (e.g. from C++11 to C++14) which is not an extension, because for example there are also existing things changed or removed, and there are compiler specific extension. That what we are talking about. These are not part of any part of the C++ standard, but some compiler support them, but not all. – gerum Jul 26 '22 at 07:25
  • @gerum Alright I don't know if I'm following. When I use the `-pedantic` command in g++ I get `warning: variable length arrays are a C99 feature [-Wvla-extension]`. Does that mean that they are part of the C standard, but not part of C++? – ATLayne Jul 26 '22 at 15:23
  • Yes, this special C++ extension is part of the C standard (which is the reason why multiple compilers have it) but from view of C++ it is nevertheless an extension. – gerum Jul 26 '22 at 19:46

0 Answers0