5

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;
}
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. Commented Apr 21, 2021 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. Commented Apr 21, 2021 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. Commented Apr 21, 2021 at 17:58
  • @user4581301 and at some point one will need to compile it using another compiler without VLA support. Commented Apr 21, 2021 at 18:01
  • 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. Commented Apr 21, 2021 at 18:09

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

An extension to the above: -pedantic warns of use of ALL of GCC's non-Standard extensions.
...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."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.