Microsoft Compilers And Dynamic Multi-Dimensional Arrays

The latest Microsoft compiler I have is VC9 from VS 2008 Pro. First question -Was wondering if on Microsoft's later compilers from VS 2010 or VS 2012 if they changed anything to allow this to compile ... ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// cl Ar2.cpp
// g++ Ar2.cpp -oAr1_x64.exe -mconsole -m64
#include <cstdio>

int main(void)
{
 int x,y;

 y=3, x=4;
 int iAr[y][x];
 getchar();

 return 0;
}

/*
C:\Code\VStudio\VC++9\64_Bit\Arrays>cl Ar2.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

Ar2.cpp
Ar2.cpp(9) : error C2057: expected constant expression
Ar2.cpp(9) : error C2466: cannot allocate an array of constant size 0
Ar2.cpp(9) : error C2057: expected constant expression
Ar2.cpp(9) : error C2466: cannot allocate an array of constant size 0
Ar2.cpp(9) : error C2087: 'iAr' : missing subscript
Ar2.cpp(9) : error C2133: 'iAr' : unknown size

C:\Code\VStudio\VC++9\64_Bit\Arrays>
*/


This sort of thing compiles and works fine on any MinGW compiler I have, but not on any Microsoft compiler all the way back to VC6. From what research I've done, I've gathered the C++ standard doesn't require dynamic array allocation, and so Microsoft chose not to support it, while for whatever reason the GNU C++ compiler does. Second question - Is that a fair description of the issue?
Last edited on
VLAs are a C99 feature that has not yet officially been pulled into C++ yet. The C++ alternatives are std::array and std::dynarray which compilers are encouraged to store on the stack instead of the heap.

Microsoft is notorious for not moving on from C89 to C99, so until they do you will be unable to use some C99 or C11 features. Interestingly, they are already adopting some C++14 features despite having incomplete C++11 support.
Thanks LB!

fred
Topic archived. No new replies allowed.