[cfe-dev] Issue with variable length arrays and C++: differences between g++ and clang

Chris Eldred via cfe-dev cfe-dev at lists.llvm.org
Thu Jul 28 08:57:26 PDT 2016


Hey Clang Users,

I am having issues compiling the follow piece of code:

#include <stdlib.h>

int main() {
    int x = 8;
    int y = 8;
    double (*testptr)[y][2][2] = (double (*)[y][2][2])
malloc(sizeof(double[x][y][2][2]));
    free(testptr);
    return 0;
}

When I run
clang test.cxx -lstdc++ -pedantic
it FAILS with
test.cxx:6:14: error: cannot initialize a variable of type 'double
(*)[y][2][2]' with an rvalue of type 'double (*)[y][2][2]'
    double (*testptr)[y][2][2] = (double (*)[y][2][2])
malloc(sizeof(double[x][y][2][2]));

(and also throws warnings about VLA usage) and as expected, when I
force the language to be C using
clang -x c test.cxx -lstdc++ -pedantic
it succeeds.

However, the corresponding gcc/g++ commands both succeed:
gcc test.cxx -pedantic
g++ test.cxx -pedantic
both succeed (although g++ throws warning about VLA usage, as expected).

I am using clang version 3.6.2-1 on Ubuntu.

Is there any way to make clang behave like g++ in this case? Am I
missing something?

Thanks,
Chris Eldred

-- 
Chris Eldred
Postdoctoral Fellow, LAGA, University of Paris 13
PhD, Atmospheric Science, Colorado State University, 2015
DOE Computational Science Graduate Fellow (Alumni)
B.S. Applied Computational Physics, Carnegie Mellon University, 2009
chris.eldred at gmail.com



More information about the cfe-dev mailing list