Reputation: 139
When I use a GCC compiler, i could dynamically allocate the array size in a function using the function input arguments, but the same fails when I compile it using Windows cl.exe compiler. Why.?
void func1(int array_ele)
{
int arr[array_ele];
for (int i=0; i<array_ele; i++)
arr[i] = i;
/* Rest of the code */
}
int main(int argc, char* argv[])
{
int a = 10;
func1(a);
/* Rest of the code */
}
Upvotes: 1
Views: 353
Reputation: 133929
int arr[array_ele];
is a variable-length array declaration. It is standard C (since C99). However, cl.exe
is not a standard-compliant C compiler, and Microsoft has basically stated that they do not intend to produce a standard-compliant C compiler (one could assume malicious intent and say they do not want to help writing portable code), focusing instead on implementing C++ features. More discussion about Visual Studio support for C standards at Visual Studio support for new C / C++ standards?
Some features of the (by now and then obsoleted) C99 are supported on VS2015 (including library support, and variable declarations anywhere within a block etc); and some standard library functions are implemented: C++11/14/17 Features In VS 2015 RTM says that:
Visual Studio 2015 fully implements the C99 Standard Library, with the exception of any library features that depend on compiler features not yet supported by the Visual C++ compiler (for example,
<tgmath.h>
is not implemented).
However as far as I know, much of the C99 - let alone C11 - compiler features remain unimplemented.
Upvotes: 4
Reputation: 409186
Variable-length arrays was introduced in the C99 standard, and the Microsoft C compiler haven't supported C99 until recently, and its support is still incomplete even in the latest 2015 edition (for example it still doesn't support VLAs).
GCC before version 5 by default used "gnu90" as C-dialect to use, which is an updated version of the C89 standard with GCC extensions, and one of those extensions is VLAs. From version 5 and later the default is "gnu11" which is the latest C11 standard with GCC extensions.
Upvotes: 4
Reputation: 206607
int arr[array_ele];
is supported in C99 and later. This is called a variable length array. gcc
supports it. Microsoft Visual Studio compilers don't seem to support it. See https://msdn.microsoft.com/en-us/library/zb1574zs.aspx.
Upvotes: 2