Search code examples
c++arraysc++11gccvariable-length-array

Can I use a C Variable Length Array in C++03 and C++11?


C has a really cool feature called variable length arrays. Its available in C90 and above, and it allows deferring the size of the array until runtime. See GCC's manual 6.19 Arrays of Variable Length.

I'm working in C++. At std=c++11, I'm catching a compile failure due to the use of alloca under Cygwin. I want to switch to variable length arrays, if possible. I also want to try and avoid std::vector and std::array because I want to stay out of the memory manager. I believe every little bit helps, so I'm happy to take these opportunities (that some folks consider peepholes).

Can I use a variable length array in C++03 and C++11?


Solution

  • VLAs are not in standard C++03 or C++11, so you'll better avoid them if you want to write strictly standard conforming code (and use e.g. std::vector, which generally use heap for its data - but you might use your own allocator...).

    However, several C++ compilers (recent GCC & Clang) are accepting VLAs as an extension.

    It is the same for flexible array members; they are not standard in C++ (only in C) but some compilers accept them.

    dynarray-s did not get into the C++11 standard...