1 / 3
Feb 2021

I read on GeeksforGeeks that arrays in c++ are statically allocated memory i.e. at the compile time.
Whereas Vectors in C++ are allocated memory dynamically.
E.g. vector vec;
vec.push_back(1);
But I have some doubt in this.
Like if I declare array like :->
int n;
cin>>n;
int arr[n];
How can this be statically allocated because at runtime only we will provide the value for n and then only memory will get allocated. Shouldn’t this come under dynamic allocation?

I might be asking very silly question but I couldn’t find any good explanation on net.
Thx in advance.! :blush:

  • created

    Feb '21
  • last reply

    Feb '21
  • 2

    replies

  • 442

    views

  • 2

    users

You cannot allocate array in C++ like this: int arr[n].
Dynamic allocation in C++ is done by new keyword.