Confused about different methods to get arrays of user defined size in c++

I am learning c++ and so far I know these ways to get an array of user defined size in cpp.

Method 1:

int main(){
int size;
cin>>size;
int arr[size];
}

What happens in the background? Since size is an int, does the compiler reserve ~2GB space on stack in advance until user input?

How does this compare to creating an array with a size equal to whatever upper bound is of constraints in question? Like see how a[maxn] is declared in setter’s solution here

Which is better? Why?

Method 2:

int main(){
int size;
 cin>>size;
int *arr = new int[size];
...
delete []arr;
}

I was wondering which one of these is the best/safe/good practice/recommended. I am aware of vector. I am looking for ways other than that - for learning purposes.

If there is some place - article/video where I can get a better understanding of this, please link it here.

1 Like

Method 2 will use heap. So you can create array of larger sizes. While Method 1 will fail for large array sizes. So Method 2 won’t fail anywhere. That’s what i know :slightly_smiling_face:

1 Like