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

5
  • I am a begineer so please try to answer in easiest way. Commented Mar 4, 2017 at 5:59
  • which error coming...? Commented Mar 4, 2017 at 6:08
  • 1
    std::vector<int> 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. Commented Mar 4, 2017 at 7:01
  • The error is :- "Can't initialize a constant with a variable value" Commented Mar 4, 2017 at 11:03
  • 1
    Possible duplicate of How do I find the length of an array? Commented Apr 27, 2019 at 19:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Are vector included in standard C++ library
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.
Can you explain me about. size_t datatype
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)
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

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.