Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7942 Discussions

variable length arrays error in windows even if gnu++98 is set

Milind_Kulkarni__Int
New Contributor II
585 Views

In the Windows Compiler documentation, it is stated that this ISO C++ feature is supported , atleast if we use flags forGnu and ISO standard. In Linux*, its allowed starting from 11.1 compiler, whereas its an error in Windows*.

When will this feature be implemented in Windows*. And what other GNU extension features are present that are supported in Linux* , but are not supported in Windows*. Please provide the list if its documented.
When porting code fromgcc to Windows* , the customers are facing lot of problems, as it may need lot of modification.

What all GNU features will be implemented in Intel Compiler for Windows in future releases.

0 Kudos
11 Replies
Milind_Kulkarni__Int
New Contributor II
585 Views
Small test-case to see difference in Linux and Windows:--

[bash]#include 
#include   

  int 
     concat_fopen (char *s1, char *s2, char *mode)
     {
       char str[strlen (s1) + strlen (s2) + 1];
       strcpy (str, s1);
       strcat (str, s2);
       return 1;
     }
[/bash]


In Windows, it gives error.

While in Linux 11.1 compiler, it works (w/o any flag).

While, using gcc -pedantic provides a suitable warning:--

warning: ISO C++ forbids variable-size array `str'
0 Kudos
Judith_W_Intel
Employee
585 Views
> In the Windows Compiler documentation, it is stated that this ISO C++ feature is supported , atleast if we > use flags forGnu and ISO standard. In Linux*, its allowed starting from 11.1 compiler, whereas its an
> error in Windows*.

Can you please point to the documentation? Variable length arrays is *NOT* an ISO C++ feature. It is a C99 feature, and is supported under the /Qstd=c99 option (when you compile a C program).

> When will this feature be implemented in Windows*. And what other GNU extension features are present > that are supported in Linux* , but are not supported in Windows*. Please provide the list if its documented.

Our goal is to becompatible with Microsoft on Windows. There are no gnu compatibility flags and no plans to support any Gnu specificextensions on Windows.
0 Kudos
Milind_Kulkarni__Int
New Contributor II
585 Views

Yes, its listed as a C99 feature in the compiler documentation. I thought it to be included in ISO C++, though the doc lists that specifically as c99 . And variable-length arrays is in the list.

However, the code that I posted was tried with /Qstd=c99 flag, and the following error is emitted:--

C:\Documents and Settings\mkulka3\My Documents\Intel-Docs\KBstuff\462642>icl /Qs

td=c99 vararr.cpp

Intel C++ Compiler for applications running on IA-32, Version 11.1 Build 2

0090421 Package ID: composer.061

Copyright (C) 1985-2009 Intel Corporation. All rights reserved.

vararr.cpp

vararr.cpp(7): error: function call is not allowed in a constant expression

char str[strlen (s1) + strlen (s2) + 1];

^

vararr.cpp(7): error: expression must have a constant value

char str[strlen (s1) + strlen (s2) + 1];

^

vararr.cpp(7): error: function call is not allowed in a constant expression

char str[strlen (s1) + strlen (s2) + 1];

^

vararr.cpp(7): error: expression must have a constant value

char str[strlen (s1) + strlen (s2) + 1];
///////////////////////////////////

It works well under Linux, so I suppose this should be escalated for Windows. Please let me know .

Thanks

0 Kudos
jimdempseyatthecove
Honored Contributor III
585 Views
Under Windows consider using _alloca

char* str = (char*)_alloca(strlen (s1) + strlen (s2) + 1);

Jim
0 Kudos
TimP
Honored Contributor III
584 Views

Yes, its listed as a C99 feature in the compiler documentation. I thought it to be included in ISO C++, though the doc lists that specifically as c99 . And variable-length arrays is in the list.

Unfortunately, in compiler option lists, ISO or "ansi" options without qualification are likely to refer to C89 or C90, even though C99 has superseded them. We are only approaching the time when a future release of gcc may default to what is now -std=c99 then we will have a new round of confusion.
0 Kudos
Milind_Kulkarni__Int
New Contributor II
584 Views
Actually, would char* str be variable length array? It seemed I should have been able to use it like char str[] , or may be I am wrong.

But, if Windows* compiler need to be compatible with Microsoft*, then the code posted by me not working could be fine, since the cl compiler also does report the same. But I do not know the /Qstd=c99 equivalent of icl for the cl .

Is there any option for this which we can apply for cl compiler

thanks
0 Kudos
jimdempseyatthecove
Honored Contributor III
584 Views
>>would char* str be variable length array? It seemed I should have been able to use it like char str[]

char* str = (char*)_alloca(10); // buffer of 10 chars

can be accessed as str[0] through str[9] as intended
and accessed str[-n] and str[9+m] as unintended

_alloca allocates off of stack at run time.
gnu permits runtime array size allocation off stack whereas Windows "standard" does not.
I do not know why /Qstd=c99 did not enable this functionality

There are other quirks with gnu such as structure declaration with initialization using ".member=value" with no structure name. Unfortunately this requires use of #if #else #endif or use of clever macros.

Jim
0 Kudos
Milind_Kulkarni__Int
New Contributor II
584 Views

Thanks I got it now.

Statement like char arr where n is variable will work with gnu compilers, and Windows* compilers (almost all) did not enable this functionality (runtime allocation on STACK) , even as a part of c99 feature.
So in Windows*, we have to use _alloca , or malloc (dynamic on heap) , though the array indices can be flexible

That helps.

0 Kudos
jimdempseyatthecove
Honored Contributor III
584 Views
Another option you have is to allocate a size larger than worst case. Example:

char str[MAXPATH];

But then use the str... functions than enforce buffer overflow testing.

Jim
0 Kudos
Judith_W_Intel
Employee
584 Views
C99 is a C standard not aC++ standard. Therefore using the /Qstd=c99 flag when compiling C++ is useless. The driver should probably give a warning about it being ignored.
0 Kudos
Judith_W_Intel
Employee
584 Views
I think the C++ committee never added variable length arrays to the language because they expected C++ users to user the (more extensible andbetter error checked) standard library vector type.
0 Kudos
Reply