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.

Saud Khan
  • 41
  • 3
  • 3
    That code is not standard compliant (and doesn't work on e.g.: Visual Studio's C++ compiler) – UnholySheep Aug 15 '21 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). – fabian Aug 15 '21 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`. – HolyBlackCat Aug 15 '21 at 12:42
  • 1
    See also [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Lukas-T Aug 15 '21 at 12:42
  • Okay. So it is not standard. But, apart from this, is there any other problem or it will work the same as Dynamic Array? – Saud Khan Aug 15 '21 at 12:51
  • 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. – Quimby Aug 15 '21 at 12:56

2 Answers2

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
k hemanth
  • 49
  • 4
  • This does not answer why the code provided in the question is not enough for this case – Lala5th Aug 15 '21 at 12:49
  • 1
    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. – k hemanth Aug 15 '21 at 12:53
  • 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 – Lala5th Aug 15 '21 at 12:54
  • The code given in question only solves the problem if the size of the array is known beforehand and the array size is constant. – k hemanth Aug 15 '21 at 12:56
  • Can we use the piece of Code that I provided in Question as an Alternative to Dynamic Arrays. if not then Why? – Saud Khan Aug 15 '21 at 12:59
  • If we want to increase the size of array from N to M (M > N). The only way to do it is to allocate more memory. If you are using vector like cin>>n; vector values(n); You can do values.resize(m); – k hemanth Aug 15 '21 at 13:01
  • `cin>>i` is not a compile time constant, hence if the code compiles (which it does on some toolchains), it is (technically) a replacement. Your answer does not address the important part of the question – Lala5th Aug 15 '21 at 13:02
  • If we want to allocate memory only once we can do it via code given in the question. But if we want to change the array size from N to M in future we need dynamic allocation to add (M-N extra size) – k hemanth Aug 15 '21 at 13:06
  • For the scenario of allocating n numbers(Where n is known at compile or runtime). we don't need dynamic array. Assume a scenario where we don't know the input size. we need to add elements given in the input to an array until 0 is given in the input. As we don't know the input size we might allocate some 100 size to array. then if the size of input is more than 100 we need to increase array size then we need dynamic arrays. – k hemanth Aug 15 '21 at 13:13
  • Assume in this question https://www.spoj.com/problems/TEST/ They want the all the elements to be printed in increasing order. then we need to store elements till 42 is read and then sort the array and then output. As we don't know the size of input we cannot allocate the size correctly which requires resize which needs dynamic array or vector to code the modified question – k hemanth Aug 15 '21 at 13:16
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];
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19