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.
int arr[x];withxnot being a compile time constant is a non-standard extension to C++. I recommend avoiding this unless you're sure all compilers you'll ever want to compile the code with support this feature (and even then this may confuse other people that are used to different compilers not supporting this).-std=c++20 -pedantic-errorswould work (or a lower version if your compiler doesn't supportc++20). Also enable warnings with-Wall -Wextra.std::vector. Also VLAs are AFAIK implemented on stack(likealloca) so they are quite limited in size.