2

I was taught that if we define array[N], then N should be a const variable or a const expression. But now i find the following code can be compiled and run correctly.(I use g++ 4.8.3, if i use vs2010, there will be a compiled error:error C2057: expected constannt express)

#include<iostream>
int main()
{
    int N;
    std::cin>>N;
    int A[N];
    for(int i=0;i<N;++i)
       std::cin>>A[i];
    for(int i=0;i<N;++i)
        std::cout<<A[i]<<" ";
    return 0;
}

Obviously N is not const type. I use g++ 4.8.3

Roger.Kang
  • 62
  • 1
  • 5
  • 4
    Try what happens when you use the `-pedantic` compiler flag. And read up about GCC default options. – Konrad Rudolph Mar 08 '15 at 12:34
  • 3
    GCC supports VLA's which are not part of the c++ standard definition. – πάντα ῥεῖ Mar 08 '15 at 12:35
  • @KonradRudolph thank you, now i know the reason. – Roger.Kang Mar 08 '15 at 12:43
  • https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html it's an extension in GCC – phuclv Mar 08 '15 at 12:43
  • It often helps to picture an N-sized non-dynamically-allocated array as an alternative way of declaring N individual variables. Defining N at run-time does not really make sense in either case, even though GCC for some reason offers it for arrays as a non-standard feature. – Christian Hackl Mar 08 '15 at 12:49
  • @ChristianHackl: VLAs avoid the inefficiency of general dynamic allocation. When this was discussed in comp.std.c++ many years ago, an argument was made that one could achieve the same effect by just having some fixed size area of storage where one implemented LIFO allocation scheme. However, on modern general computers the stack area is not fixed size, but can grow (very efficiently), and there's one stack per thread. Thus nearly all compilers support the *de facto* standard `alloca` function. However, the failure handling for `alloca` is not well defined, and it's too low level for C++. – Cheers and hth. - Alf Mar 08 '15 at 13:22

2 Answers2

0

"Does array size need to be const?"

Yes, according the current c++ standard, array size specifiers need to be const expressions.

A number of compilers support VLA's (variable length arrays) allocation on the stack. It's a compiler extension and isn't portable code.

If you need a portable variant, you can use a std::vector<int> like follows

int N;
std::cin>>N;
std::vector<int> A(N);
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Apparently there are two proposals for VLAs in C++. See (https://isocpp.org/blog/2013/04/n3639-runtime-sized-arrays-with-automatic-storage-duration). None of them made it into C++14. – Cheers and hth. - Alf Mar 08 '15 at 12:54
0

It seems that the first compiler has its own language extension that allows to use Variable Length Arrays (that are introduced in C).

Nevertheless this feature is not C++ compliant. According to the definition of arrays in C++ the size of an array if it is present shall be a constant expression greater than zero:

D1 [ constant-expressionopt] attribute-specifier-seqopt

From the C++ Standard (8.3.4 Arrays)

  1. ...If the constant-expression (5.19) is present, it shall be a converted constant expression of type std::size_t and its value shall be greater than zero.
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335