When are variable-length arrays legal?

前端 未结 7 1213
南笙
南笙 2020-12-21 05:30

I\'m not a C++ expert, but as far as I know this code should fail due to size not being constant:

#include

using namespace std;         


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-21 06:09

    c99 supports variable length arrays(VLA) but neither c90 nor C++ supports variable length arrays, but gcc support this as an extension in both C and C++ you can see this more clearly if you compile with these arguments:

    gcc -std=c89 -pedantic
    

    this will give you the following warning:

    warning: ISO C90 forbids variable length array ‘array’ [-Wvla]
    

    or with g++:

    g++ -pedantic
    

    will give you this warning:

    warning: ISO C++ forbids variable length array ‘array’ [-Wvla]
    

    this standards section in the gcc manual goes into more details. Important to note that as of the 2011 C standard variable length arrays(VLA) are now optional.

提交回复
热议问题