0

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?

Raj
  • 664
  • 4
  • 16
  • 4
    Use the standard and portable [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead of non-standard and non-portable [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). – Some programmer dude Jun 12 '19 at 05: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? – Raj Jun 12 '19 at 06: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. – Hans Passant Jun 12 '19 at 06:04
  • 1
    Possible duplicate of [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) – Miles Budnek Jun 12 '19 at 06:12
  • @Raj [HERE](https://stackoverflow.com/questions/381621/using-arrays-or-stdvectors-in-c-whats-the-performance-gap) you can find all sorts of information why you shouldn't go after your plan =) – skratchi.at Jun 12 '19 at 06:15
  • ***Why*** don't you want to allocate on the heap? What is the *real* problem you need to solve? This seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Some programmer dude Jun 12 '19 at 06:57

2 Answers2

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

Fureeish
  • 12,533
  • 4
  • 32
  • 62
Oblivion
  • 7,176
  • 2
  • 14
  • 33
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

bahij zeineh
  • 228
  • 2
  • 11