Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need of Dynamic Array in C++

We create Dynamic Array when we don't know the exact size of input at compile time, right? But can't we solve this problem without using Dynamic arrays? For Example:

cout<<"Enter Size of Array";
cin>>x;
int arr[x];

By using above piece of code we can create an int Array and the Size of Array depends upon User Input (i.e x). If this Code Solves our problem then what is the need of Dynamic Array?

I am new in programming, So try to explain it Simply. Thanks.

like image 512
Saud Khan Avatar asked Sep 20 '25 11:09

Saud Khan


1 Answers

  • Dynamic arrays are used when the size of array is not known before hand or not user inputted.

For example, We want store the details of the users subscribed to a product updates.

In this case we don't know how many users will subscribe to the product updates. So we will need dynamic arrays or we can use vectors in C++.

Generally in competetive coding we don't need dynamic arrays as we know the input before hand. But in real world use cases we might need the dynamic arrays.

Additional references:

  • How vector works internally - link
like image 127
k hemanth Avatar answered Sep 23 '25 02:09

k hemanth