1

I was just going through some simple C++ concepts. I like to think that I am aware of the difference between dynamic and static arrays. But when I run the following code:

`

#include <iostream>
using namespace std;
int main()
{
    int size;
    cout<<"enter size: ";
    cin>>size;
    int arr[size];
    cout<<"enter array values: ";
    for(int i=0;i<size;i++)
        cin>>arr[i];
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    return 0;
    }

`

It does not give me an error. It shouldn't let me create a static array with the size input from the user right?

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
ahmach98
  • 11
  • 2
  • 3
    C++ doesn't support C VLAa (*Variable Length Arrays*) -- except by specific non-standard compiler extensions (g++ does). The `size` must be a literal constant known at **compile-time**, not determined at **run-time**. – David C. Rankin Mar 18 '20 at 20:44
  • 1
    Some compilers accept this code, as an extension to the C++ language. – spectras Mar 18 '20 at 20:45
  • That's most likely a compiler extension. Try the flag `-pedantic` or `/permissive-`. – Timo Mar 18 '20 at 20:45
  • You are probably using a compiler that adds an 'extension' to the C++ standard, allowing the use of VLAs (g++ is one, I think). – Adrian Mole Mar 18 '20 at 20:45
  • 3
    [Why should I always enable compiler warnings?](https://stackoverflow.com/a/57842757/430766) – bitmask Mar 18 '20 at 20:47
  • 3
    Also, while convenient for short example programs, make sure you read [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696). Starting with good habits leaves nothing that will be difficult to break later `:)` – David C. Rankin Mar 18 '20 at 20:52
  • The question should be: Why can I create a static array dynamically in C++ using a compiler that has a non-standard extension to create static arrays dynamically? – Eljay Mar 18 '20 at 20:57
  • 1
    Related: [https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – drescherjm Mar 18 '20 at 21:01

1 Answers1

5

You cannot. This is not valid C++.

It compiles because the compiler you use offers extensions beyond by the C++ standard. Your compiler would have warned you about it if you had enabled compiler warnings:

-Wall -Wextra -pedantic
bitmask
  • 32,434
  • 14
  • 99
  • 159
  • Note that enabling just any warnings (such as -Wall or -Wextra) is not sufficient. It is necessary to use -pedantic to enable standard conformance. – eerorika Mar 18 '20 at 21:07
  • @eerorika Thanks. Amended. – bitmask Mar 18 '20 at 21:09
  • ... and sometimes that's not even enough. `-pedantic-errors` seems to help with the last bit. – Ted Lyngmo Mar 18 '20 at 23:38
  • @TedLyngmo `-Werror` and sorts are always alluring, but if one gets in the habit of visually reading warnings as errors it becomes unnecessary. Further, there are sometimes situations during debugging where you want stuff like unused arguments and friends to be displayed as warnings but to not break the build. – bitmask Mar 18 '20 at 23:45
  • I got over that and usually just fix it instead of forcing the compilation through. If I don't agree with the analysis, I either remove that particular analysis step completely or for the single case by adding a comment why the code is better as-is than it would be as suggested by the linter or whatever it is that is complaining. – Ted Lyngmo Mar 19 '20 at 00:06