0

From A Tour of C++

The size of an array must be a constant expression

I can not understand why this compiles and runs without errors

int main() 
{
    int input;
    cin >> input;
    char v[input];
    cout << sizeof(v) << endl;
}

While this doesn't

int value(int value)
{
    for(int i = 0; i < value; i++) {
        value++;       
    }
    return value;
}

int main() 
{
    int input;
    cin >> input;
    char v[value(input)];
    cout << sizeof(v) << endl;
}

Can somebody explain to me why does this happen?

tornikeo
  • 915
  • 5
  • 20
  • Try that code with the Microsoft C++ compiler. – PaulMcKenzie Aug 29 '20 at 18:36
  • I am getting this on Ubuntu Linux. How is OS relevant in this example? O.o – tornikeo Aug 29 '20 at 18:38
  • 1
    1) "_I can not understand why this compiles and runs without errors_" You are using the compiler, that enables such behavior, with an extension. 2) "_How is OS relevant in this example?_" OS isn't. MSVC is one of the compilers, that doesn't have such extension. – Algirdas Preidžius Aug 29 '20 at 18:38
  • It isn't the operating system, it is the compiler. – PaulMcKenzie Aug 29 '20 at 18:38
  • "g++ --version g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0", what extensions? – tornikeo Aug 29 '20 at 18:38
  • See the duplicate link. The code is not valid C++, and can be flagged by the compiler you're using if you use the appropriate compiler options. The Microsoft compilers never accepted this syntax, and probably never will -- that's why I mentioned to you to try that code with any Visual C++ compiler. – PaulMcKenzie Aug 29 '20 at 18:40
  • @tornikeo "what extensions" G++ is one of those compilers, that has a compiler extension enabled by default, that enables VLAs, even if they never been a part of C++ standard. – Algirdas Preidžius Aug 29 '20 at 18:40
  • 1
    Two tips. (1) Pick a standard explicitly when compiling, I prefer `-std=c++17` currently. (2) Make the compiler *really* stick to it, `-pedanitc-errors`. – StoryTeller - Unslander Monica Aug 29 '20 at 18:40
  • @StoryTeller-UnslanderMonica There's a typo in your compiler flag suggestion: `-pedanitc-errors` -> `-pedantic-errors` :) – Algirdas Preidžius Aug 29 '20 at 18:43
  • @StoryTeller-UnslanderMonica Thanks! Now the output makes sense. Why is that kind of thing even allowed, It could lead to pretty bad bugs down the line. – tornikeo Aug 29 '20 at 18:43
  • 1
    @tornikeo "_Why is that kind of thing even allowed_" Ask GCC development team, but my guess is: backwards compatibility/ease of moving from C (which started back in the day, that later moved towards backwards compatibility) thing. – Algirdas Preidžius Aug 29 '20 at 18:45
  • 1
    It's allowed because C has standard VLAs. And they have been proposed for C++ in the past (but as of yet were not accepted). So some compilers implement them, and some folks find it a useful extension. GCC usually allows extension by default because many popular projects need them for various reasons. It is my opinion that any new project should specify strict standard adherence by default, so as to not be caught unawares when porting or changing tool-chains. – StoryTeller - Unslander Monica Aug 29 '20 at 18:46
  • 1
    It's easy to add things to a language but it's hard to remove them, because then you break other people's code. Things that are now considered harmful but are still in use can't be removed on a whim. You could argue that changing defaults is fine, though that still creates work for a lot of other people. – cdhowie Aug 29 '20 at 18:49

0 Answers0