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 ?

10
  • 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. Commented Feb 4, 2019 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. Commented Feb 4, 2019 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. Commented Feb 4, 2019 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. Commented Feb 4, 2019 at 16:15
  • 1
    adding vlas to C99 doesnt make them valid in C++. C and C++ are two different languages, if that caused your confusion Commented Feb 4, 2019 at 16:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.