4

Following code compiles fine since g++ allows it , but will it cause undefined behavior ? Or my code will work fine ? what does it mean that c++ standard disallows variable length array if no error is generated on using it ?

#include <iostream>

using namespace std;

int main()
{
   int x;
   cin >> x;
   char abc[x];
   cout << sizeof(abc) << "\n";
   
   return 0;
}
spooja__
  • 203
  • 2
  • 7
  • 5
    What happens should be documented in the g++ documentation. My _guess_ is that it'll follow the rules for VLA:s in C code pretty closely. – Ted Lyngmo Apr 21 '21 at 17:51
  • According to the C++ standard, the behavior for this code is undefined. However, C++ implementations such as GCC may provide non-standard extensions of C++, such as VLA support. Then, the behavior is undefined by the standard but may be defined by that particular implementation. – Daniel Langr Apr 21 '21 at 17:58
  • 1
    They do, but note that the C rules for VLAs are not the same as the C rules for regular, fixed size arrays. Initialization is very different and they, my opinion, make a mess of the `sizeof` operator. The C++ Standard is open to extensions, and GCC's VLA support is such an extension. The behaviour is well defined, though I'll admit to not being a fan of the behaviour and recommend not using VLA. They are an extremely easy way to insert bugs into a program, particularly when you allow a user to specify the size. – user4581301 Apr 21 '21 at 17:58
  • @user4581301 and at some point one will need to compile it using another compiler without VLA support. – Suthiro Apr 21 '21 at 18:01
  • I once found a better [documentation link than this](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html), but today it seems to be the best Google deems me worthy of seeing. – user4581301 Apr 21 '21 at 18:03
  • 3
    The big and nasty initialization difference: `int arr[x] = {0};` Initializes the first element to 0. If `x` is a compile-time constant, the rest of the array is also zeroed. If `x` is not a compile-time constant, the rest of the array is left containing whatever garbage happened to be where the array now lives in storage. This is a nasty surprise to many. – user4581301 Apr 21 '21 at 18:09
  • Some extra reading on the subject: [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) – user4581301 Apr 21 '21 at 18:13

1 Answers1

7

GCC documents its support of VLAs here (emphasis mine)

Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++.

CLANG documentation too follows suit but mentions clearly that the standard doesn't accept (emphasis mine)

GCC and C99 allow an array's size to be determined at run time. This extension is not permitted in standard C++. However, Clang supports such variable length arrays for compatibility with GNU C and C99 programs.

If you would prefer not to use this extension, you can always disable it with -Werror=vla to disallow compilation.

Zoso
  • 3,273
  • 1
  • 16
  • 27
  • 2
    An extension to the above: `-pedantic` warns of use of ALL of GCC's non-Standard extensions. – user4581301 Apr 21 '21 at 18:04
  • 1
    ...and throw in `-pedantic-errors` for good measure. "_This is not equivalent to `-Werror=pedantic`, since there are errors enabled by this option and not enabled by the latter and vice versa._" – Ted Lyngmo Apr 21 '21 at 18:36