0

I want to initialize an array with user defined size, but what know is- I've to declare an array of maximum size and then work on number of elements given by user in this process huge memory wastes. Is there any way to declare an array of size given by user. I did this but compiler showed error.

int a=0;
std::cout<<"Enter size of array";
std::cin>>a;
const int b=a;
int ary[b];

I'm using Turbo C++IDE

Snigdh
  • 61
  • 11
  • I am a begineer so please try to answer in easiest way. – Snigdh Mar 04 '17 at 05:59
  • which error coming...? – Darshak Mar 04 '17 at 06:08
  • 1
    `std::vector ary(b);` -- And why are you using `Turbo C++`?? That compiler is 25 years old, from another era, and is nowhere close to the current ANSI C++ standard. There are plenty of free compilers that are up-to date. – PaulMcKenzie Mar 04 '17 at 07:01
  • The error is :- "Can't initialize a constant with a variable value" – Snigdh Mar 04 '17 at 11:03
  • 1
    Possible duplicate of [How do I find the length of an array?](https://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array) – Omar Sherif Apr 27 '19 at 19:38

2 Answers2

2

The issue with your code is that you are declaring what is called a variable length array which is not part of C++ (though it is valid C code). See this for an explanation as to why.

You can achieve what you are trying to do in a few different ways though:

You could dynamically allocate an array using a user provided size:

#include <iostream>
#include <memory>

int main(int argc, char** argv)
{
    std::size_t a =0;
    std::cout<<"Enter size of array";
    std::cin>>a;

    std::unique_ptr<int[]> arr(new int[a]);
    //do something with arr
    //the unique ptr will delete the memory when it goes out of scope

}

This approach will work but may not always be ideal, especially in situations where the size of the array may need to change frequently. In that case I would recommend using a std::vector:

#include <iostream>
#include <vector>

int main(int argc, char** argv)
{
    std::size_t a =0;
    std::cout<<"Enter size of array";
    std::cin>>a;

    std::vector<int> arr(a);//arr will have a starting size of a
    //use arr for something
    //all of the memory is managed internally by the vector
}

You can find the reference page here.

Alex Zywicki
  • 2,263
  • 1
  • 19
  • 34
  • Are vector included in standard C++ library – Snigdh Mar 04 '17 at 11:56
  • Yes, both solutions I provided are part of the standard library specifically the STL (standard template library) all that is required to use them is including the headers that I showed in my examples. – Alex Zywicki Mar 04 '17 at 17:17
  • Can you explain me about. size_t datatype – Snigdh Mar 04 '17 at 17:36
  • std::size_t is a tankard provided typedef for an unsigned integer of a size large enough T to represent the size of any piece of memory or index any array. It is the data type that is used by the standard library to represent the sizes of any container. I prefer to use if because it is what the standard uses for all of its size representations. You could have had a bug in your code if the user had given a negative number. That cannot happen with std::size_t(though it would still be undefined behavior due to integer over/under flow) – Alex Zywicki Mar 04 '17 at 17:42
1

You can use new keyword while declaring a dynamic array

int main()
{
  int array_size;

  std::cin >> array_size;

  int *my_array = new int[array_size];

  delete [] my_array;

  return 0;
}

You should delete the array allocated with new.

You can also use vector for dynamic allocation of memory in c++. Read here for examples on vector