Beginner's question: Dynamic array size and dynamic memory

closed account (z1vC5Di1)
Hello everybody,
I gained all my knowledge about C++ from this webside's tutorial and I am somewhat still confused why this would not work:
1
2
3
int x;
cin >> x;
int y[x];

My compiler does not complain. Still the tutorial says that in this case I should use dynamic memory:
1
2
3
int x;
cin >> x;
int *y = new int[x];

I just wonder: What is wrong with the first lines of code?
Thank you for any explenation! Best, Rafael
Last edited on
Your compiler must be using a language extension called "variable length array". It's a feature of the C language that is not present in C++, but some compilers (most notably GCC) permit it as an extension.

If you're using gcc, compile with -pedantic.
in the first code .. array needs to have the constant size as .
1
2
#define SIZE 50
int y[SIZE];
Topic archived. No new replies allowed.