Incorrect code compiles in codeblocks but not in visual studio

I have tried a few lines of code that allows the user to determine an arrays size at runtime (without using dynamic memory) and it should produce an error before compiling. However, when I compile and run in codeblocks everything works fine, I can even enter the amount of elements I want in the array. I thought this was strange so I went to visual studio and pasted the code, I wasn't even able to compile the program (the error tells me that the array's size needs to be a constant value... the normal, expected error). Someone please explain this to me, it is making me really confused when trying to understand the need for dynamic memory allocation.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;


int main()

{
    srand(time(0));
    int amountMonsters=0;
    cout << "Please enter how many monsters you want to have in the game: ";

    
    cin >> amountMonsters;
 
    int nArray[amountMonsters];




    return 0;
}
ASFAIK, that is standards compliant for C99 (which I believe isn't really used, they do weird things). So your version of G++ might support it, it seems like.
Lol its kinda nice that I dont have to allocate memory for this when using codeblocks, now I just wonder what else is it compiling that it shouldn't
I don't know what the standards say about it, but I imagine it's using some dynamic allocation underneath, anyway.

I wouldn't rely on this, as C99 is kind of not supported everywhere. As you saw with whatever compiler microsoft is using.
Topic archived. No new replies allowed.