0

I am trying to understand the difference between static and dynamic arrays in C++ and I can not think of a case where a static array would not do the trick.

I am considering a static array that would be declared this way:

    int N=10;
    int arr[N];`

I read here that the main difference between static and dynamic array is that a static array is allocated during compilation so N needs to be know at compilation.

However, this explains that arrays declared this way can also be Variable-length arrays:

Variable-length arrays were added in C99 - they behave mostly like fixed-length arrays, except that their size is established at run time; N does not have to be a compile-time constant expression:`

and indeed, the following c++ code is working, even though n is only known at runtime :

    int n =-1;
    std::cin>>n;
    int arr[n];

    //Let the user fill the array
    for(int i=0; i<n;i++){
        std::cin>>arr[i];
    }

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

So I would like an example of code were a static arrays defined like this would not work and the use of dynamic array would be required ?

Blaze
  • 16,736
  • 2
  • 25
  • 44
Mai Kar
  • 138
  • 1
  • 3
  • 18
  • 8
    Your code doesn't work on my compiler (MSVC), precisely because VLRs aren't part of C++ and only *some* compilers allow it nevertheless. In other words, the code that you provided is the example that you seek. – Blaze Feb 04 '19 at 16:11
  • 9
    C++ doesn't have Variable Length Arrays. Some compilers allow it as an extension, but it's not part of standard. – Yksisarvinen Feb 04 '19 at 16:12
  • 1
    You need to turn up your warnings. Add `-pedantic` if you are using gcc/clang and you'll definitely get an error. – NathanOliver Feb 04 '19 at 16:12
  • Don't mix your static allocation from compile time phrases up. A static variable has a life time throughout a program but an array is built on the stack if it has automatic storage. – Bayleef Feb 04 '19 at 16:15
  • And to be fair, that's already a dynamic array. But in C++, we use `std::array` and `std::vector` instead. – Matthieu Brucher Feb 04 '19 at 16:25
  • 1
    adding vlas to C99 doesnt make them valid in C++. C and C++ are two different languages, if that caused your confusion – 463035818_is_not_an_ai Feb 04 '19 at 16:25
  • @NathanOliver I tried your advice with the rather newest compilers on godbolt. [**Live Demo on godbolt**](https://gcc.godbolt.org/z/eAav8S) With `-pedantic`, I got at least a warning in `gcc` and `clang`. (With `-pedantic-errors`, it became an error of course.) MSCV denied with error (as already mentioned by @Blaze). – Scheff's Cat Feb 04 '19 at 16:25
  • @Blaze Alright thank you, that is the information I missed, and after reading [this](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard), I understand why. If you put that as an answer i'll select it. – Mai Kar Feb 04 '19 at 16:26
  • @NathanOliver I just tested the commands in an online compiler, I guess this is a good reason no to do that ^^. If I had used gcc -Wall as usual, I would have got this. – Mai Kar Feb 04 '19 at 16:28
  • Alright, I added it as an answer. I didn't originally do so because I'm sure there's a dupe for it somewhere, but I tried to add a bit more context and an explanation or two. I hope it's fine like this, and if anyone has more insight on the matter, feel free to edit it. – Blaze Feb 04 '19 at 16:43

1 Answers1

2

The code doesn't work on all compilers because variable length arrays aren't part of C++. They are part of C as of ISO C99, and some compilers will allow VLAs in C++, but it's not a portable solution. GCC, for instance, allows VLAs, but warns the user (-Wno-vla).

Below the hood, VLAs are dynamic anyway as the compiler can't reserve the appropriate amount of stack memory because it doesn't know how big the array is going to be. Instead of VLAs, std::vector can be used for dynamic memory that gets deallocated at the end of the scope.

Blaze
  • 16,736
  • 2
  • 25
  • 44