1

For example:

int size;
cout << "Enter array size" <<endl;
cin >> size;
int myarray[size];

I want the user to be able to enter the size of the array, but I keep getting an error message saying I'm not using a constant variable. When ever I search for an answer to this question, I get information about how to store a variable in an array (not what I'm looking for).

3
  • 6
    use std::vector. Commented Dec 30, 2013 at 19:52
  • 1
    use std::vector<int>. Commented Dec 30, 2013 at 19:59
  • use std::vector<int, std::allocator<int>>. Commented Dec 30, 2013 at 20:21

3 Answers 3

4

Variable length arrays are not part of the C++ standard, although they are part of the C99 standard and several compilers support them as an extension in C++ including gcc and clang, Visual Studio being one of the notable exceptions though.

The obvious C++ solution would be to use std::vector or possibly new although that means you have to worry about delete'ing the memory as well.

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

Comments

3

In C++ you can simply use std::vector, in your case a std::vector<int>.

You then need to include the <vector> header.

using std::vector;
int size;
cout << "Enter array size ";
cin >> size;
vector<int> myarray(size);

A std::vector takes care of managing memory for you, and it also has a handy push_back method to add an item at the end, expanding the array as necessary.

This means that you do not have to decide up-front on a specific size, but can simply add more items.

Comments

2

The proper way to handles this in C++03 and C++11 is to use a managed dynamic array type such as std::vector:

int size;
std::cout << "Enter array size" << std::endl;
std::cin >> size;
std::vector<int> myarray;
myarray.resize(size);

std::vector<int> behaves a lot like a raw array of ints. You can use [3] to access elements, as an example.

This is superior to managing the memory yourself, as ever experienced programmers lose track of memory.

There are proposals to add dynamically sized arrays to C++1y (the next iteration of C++) or later in an extension in a way that is somewhat compatible with C99 (an important difference is that sizeof(variable length array) in C99 is runtime evaluated, but not in C++). There are also proposals to add std::dynarray, which is a slightly less heavyweight std::vector style class with some possibility of automatic storage optimization. None of this is really needed to solve your problem.

1 Comment

I figured I would do one thing on each line. The cost (at run time) is very modest, and I figured it would be clearer for someone whose first interaction with std::vector is this post to do each step explicitly.

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.