1

I typed this code in VS Community:

#include <iostream>
using namespace std;

int main(){
    int N;
    cin >> N;
    int T[N];

    return 0;
}

and it gives me these errors :

C2131   expression did not evaluate to a constant
Error (active)      expression must have a constant value   

in line 7 (int T[N];). I really don't know what's going on.

irqize
  • 97
  • 1
  • 10
  • 4
    Variable Length Arrays are not part of Standard C++ and are only supported by various compilers as an extension to the language. Apparently your compiler does not support them. I suggest using `std::vector` instead of relying on something that should not be considered compatible across compilers. – Captain Obvlious Aug 14 '15 at 20:43
  • Start by reading about [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) then search for tutorials and/or examples and play around with it until you understand it. If you run into a problem using it, can't figure out how to perform a specific task with it, etc. make another post with all the details and we'll help. – Captain Obvlious Aug 14 '15 at 20:46
  • I have updated my answer. – sergej Aug 15 '15 at 12:42

2 Answers2

2

The visual c++ compiler does not support variable length arrays. So, the best option in this case would probably be to use a std::vector. However, if for some reasons, you are not allowed to use dynamic memory allocation, you can use _malloca() to allocate a memory block on the stack.

Here is an example:

#include <iostream>
#include <malloc.h>

int main() {
    int* T;
    size_t N = 0;
    const size_t Nmax = _ALLOCA_S_THRESHOLD / sizeof(*T);

    /**
     * read array size from the standard input,
     * return if reading fails or if the requested 
     * array size would exceed the stack size
     */
    std::cin >> N;
    if (std::cin.fail() || N > Nmax) {
        std::cout << "bad input" << std::endl;
        return -1;
    }

    T = (int*)_malloca(N * sizeof(*T));

    /** 
     * do somethig with the array
     * eg: initialize it with zeros
     */
    for (int i = 0; i < N; i++)
        T[i] = 0;

    /* free the memory block */
    _freea(T);

    return 0;
}

Related:

NOTE:

  • Use _malloca() with care, see the msdn documentation for details.
  • In debug mode, _malloca always allocates memory from the heap.
Community
  • 1
  • 1
sergej
  • 17,147
  • 6
  • 52
  • 89
  • There's a few other issues with this code. 1: reading from cin might fail. You could deal with that by either giving N a positive and non-zero value when it is created, or by surrounding the cin >> N bit with an if, etc. . Also, it's handy to check whether N is less than zero before doing any memory allocation or whatever. And you'd probably be better off with using a container (such as std::vector eg), rather than using malloc and such. Resizing a vector and that kind of thing is easier than the malloc way... –  Aug 15 '15 at 11:24
  • @Tive See the update, the `std::vector` thing has been already mentioned. – sergej Aug 15 '15 at 12:38
  • Where is a [call to `_freea`](http://stackoverflow.com/a/734878/673826)? You might introduce a memory leak if allocation happened on heap instead of stack. Especially in debug mode. – mlt Feb 20 '16 at 00:35
1

An array bound is required to be constant, which it isn't in the current code.