Vectors vs Dynamic arrays.

I find it possible to initialise arrays dynamically in C++.
By this i mean :

1
2
int n;
int ar[n];


works fine.

So what is the difference between this and vectors ?
Actually, what you wrote is illegal in C++! In C++, you cannot make a VLA or variable length array on the stack. If n was considered constant, that would be legal but then n would no longer be of variable value. The correct way to do what you're doing is to make it dynamically via new or some allocator i.e. int ar = new int[n];

There is no real comparison to that. A vector is defined as an array of data that can be resized. The idea behind your array is that it cannot be resized thus is static.
Last edited on
Topic archived. No new replies allowed.