Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are variable length arrays an extension in Clang too?

Tags:

c++11

I understand that VLAs are not part of C++11 and I have seen this slip by GCC. It is part of the reason I switched to Clang. But now I am seeing it Clang too. I am using clang 3.2 (one behind the latest) and I am compiling with -pedantic and -std=c++11

I expect my test to NOT compile yet it compiles and runs.

int myArray[ func_returning_random_int_definitely_not_constexpr( ) ];

Is this a compiler bug or an I missing something?

In response to the comment here is the random_int_function()

#include <random>
int random_int_function(int i) 
{
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1,100);

    int random_int = distribution(generator);  

    return i + random_int;
}
like image 289
Arbalest Avatar asked Jul 15 '13 01:07

Arbalest


People also ask

Does C++ support VLA?

VLA is a C99 feature but not extended to C++, because C++ says vectors almost always better then VLA, it's probably too much of work for compiler writers to implement them for the rare cases where they might be better than vectors. nevertheless some compilers like gcc supports them through compiler extensions.

What makes an array a variable length array?

In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time). In C, the VLA is said to have a variably modified type that depends on a value (see Dependent type).

Does clang support c11?

You can use Clang in C++11 mode with the -std=c++11 option. Clang's C++11 mode can be used with libc++ or with gcc's libstdc++.

Are variable sized arrays allowed in C++?

A variable length array cannot be initialized. Note: In C++ applications, storage allocated for use by variable length arrays is not released until the function they reside in completes execution. A variable length array can be the operand of a sizeof expression.


1 Answers

Yes, variable length arrays are supported in clang 3.2/3.3 contrary to the C++11 Standard (§ 8.3.4/1).

So as you say, a program such as:

#include <random>

int random_int_function(int i) 
{
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1,100);

    int random_int = distribution(generator);  

    return i + random_int;
}

int main() {
    int myArray[ random_int_function( 0 ) ];
    (void)myArray;
    return 0;
}

compiles and runs. However, with the options -pedantic; -std=c++11 that you say you passed, clang 3.2/3,3 diagnoses:

warning: variable length arrays are a C99 feature [-Wvla]

The behaviour matches that of gcc (4.7.2/4.8.1), which warns more emphatically:

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

To make the diagnostic be an error, for either compiler, pass -Werror=vla.

like image 198
Mike Kinghan Avatar answered Sep 29 '22 07:09

Mike Kinghan