4

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.

6
  • 3
    That code is not standard compliant (and doesn't work on e.g.: Visual Studio's C++ compiler) Commented Aug 15, 2021 at 12:38
  • 1
    The problem here is that int arr[x]; with x not 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). Commented Aug 15, 2021 at 12:39
  • 3
    It's a good idea to keep your compiler configured to reject non-standard C++ extensions, especially when learning. -std=c++20 -pedantic-errors would work (or a lower version if your compiler doesn't support c++20). Also enable warnings with -Wall -Wextra. Commented Aug 15, 2021 at 12:42
  • 1
    See also Why aren't variable-length arrays part of the C++ standard? Commented Aug 15, 2021 at 12:42
  • 1
    It has all the disadvantages of ordinary raw arrays - unknown size, decay to ptr, pass by value, just use std::vector. Also VLAs are AFAIK implemented on stack(like alloca) so they are quite limited in size. Commented Aug 15, 2021 at 12:56

2 Answers 2

4
  • 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
Sign up to request clarification or add additional context in comments.

10 Comments

This does not answer why the code provided in the question is not enough for this case
I think the question is what is the need of dynamic arrays. The code given will work for most of compilers. This answer seems to address the question of when dynamic arrays are used.
The question is: "What is the need of dynamic arrays IF the problem they address can be solved by this code [not using std::vector or dynamic arrays]". The important part isn't the dynamic array use, but why the code provided is not to be used
The code given in question only solves the problem if the size of the array is known beforehand and the array size is constant.
Can we use the piece of Code that I provided in Question as an Alternative to Dynamic Arrays. if not then Why?
|
0

You could use std::vector, it can almost be used as an array. And it can be initialized at runtime and if you want it can be resized later

std::cout<<"Enter Size of Array";
std::cin>>x;

//  vector also initializes all entries to 0
std::vector<int> values(x);
int answer = values[42];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.