1

I am using visual studio 2013 express and MSVC compiler.

I get an error on executing the following lines of code.

#include<iostream>
using namespace std;
int main()
{
     int n;
     cin>>n;
     int a[n];
     return 0;
}

It says expression must have a constant value on the line on which I declare array a. I searched and found this c++ array - expression must have a constant value

It says you need to turn on a compiler option to allow it. How do I set that option in Visual studio express?

6
  • 4
    Use the standard and portable std::vector instead of non-standard and non-portable variable-length arrays. Commented Jun 12, 2019 at 5:53
  • @Someprogrammerdude But using vector would allocate it on heap instead of stack. Does changing the compiler to gcc allow me to do dynamic size allocation of array? Commented Jun 12, 2019 at 6:03
  • 2
    Variable-length arrays are a controversial C99 feature that C11 had to make optional again. It is not a C++ feature and MSVC++ does not implement it at all. The non-portable alternative is _malloca(), designed to prevent the runtime mishap this site is named for when you allocate too much. Commented Jun 12, 2019 at 6:04
  • 1
    Possible duplicate of Why aren't variable-length arrays part of the C++ standard? Commented Jun 12, 2019 at 6:12
  • @Raj HERE you can find all sorts of information why you shouldn't go after your plan =) Commented Jun 12, 2019 at 6:15

2 Answers 2

4

You can use pointers

int*a = new int [n];

You have to delete before going out of your a's scope:

delete[] a;

But better use vector:

vector<int> a(n);

You can also use llvm smallvector which is optimized for small arrays with no heap allocation if the size was small

llvm::SmallVector<int, 5> smallVector;
 for(int i = 0; i < 5; i++) { 
    smallVector.push_back(i); } // No heap allocations have been performed up to this point.
     smallVector.push_back(6); // heap allocation now

But keep in mind the compiler will decide where to allocate. Smallvector

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

2 Comments

But that would allocate it on heap memory region. I want to allocate it on stack region
No other option. Clang has smal vector that might allocate in stack if n was small.
1

try the following:

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int *a=new int[n];
    delete[] a;
    return 0;
}

the way your doing it it gets allocated on the stack and for that it has to be constant whereas this way its on the heap and it can be whatever value.

i dont think there is a compiler option to change that

2 Comments

But there are performance issues when allocating it on heap instead of stack.
Please, never forget to delete the memory!

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.