int array[N]; when N is not a constant

S

Szabolcs

Is the following legal?

void fun(int N) {
int arr[N];
}

gcc and Digital Mars accept it, but msvc complains that it wants a
constant expression.
 
J

Joe Greer

Szabolcs said:
Is the following legal?

void fun(int N) {
int arr[N];
}

gcc and Digital Mars accept it, but msvc complains that it wants a
constant expression.

This would have to be an extension for these compilers. Arrays require a
integral constant expression [dcl.array] which is defined in [expr.const]
as:

"An integral constant expression can involve only literals (2.13),
enumerators, const variables or static data members of integral or
enumeration types initialized with constant expressions (8.5), nontype
template parameters of integral or enumeration types, and sizeof
expressions. Floating literals (2.13.3) can appear only if they are cast to
integral or enumeration types."

Hope that helps.

joe
 
R

red floyd

Szabolcs said:
Is the following legal?

void fun(int N) {
int arr[N];
}

gcc and Digital Mars accept it, but msvc complains that it wants a
constant expression.

It's a C99-ism. Variable length arrays (VLAs) are legal in C99, not in
C90 or C++98 or C++03.

There's an option to gcc which specifies which dialects to accept. I
don't remember what it is, you can ask in gnu.g++.help.

Again, in summary, it's not legal in C++.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Szabolcs said:
Is the following legal?

void fun(int N) {
int arr[N];
}

gcc and Digital Mars accept it, but msvc complains that it wants a
constant expression.

It's a C99-ism. Variable length arrays (VLAs) are legal in C99, not in
C90 or C++98 or C++03.

There's an option to gcc which specifies which dialects to accept. I
don't remember what it is, you can ask in gnu.g++.help.

-std=c++98, other good options are -pedantic and -Wall
 
J

Jim Langston

Szabolcs said:
Is the following legal?

void fun(int N) {
int arr[N];
}

gcc and Digital Mars accept it, but msvc complains that it wants a
constant expression.

Not currently. Some compilers, as explained, use it as an extention. It is
my understanding, however, that the next version of C++ will allow it. But
currently, it is not kosher.
 
S

Szabolcs

Erik said:
-std=c++98, other good options are -pedantic and -Wall

Thanks for the reply. I did use -std=c++98 and -Wall, but not
-pedantic. -pedantic prevents this from compilig.


Is it considered for the next version of C++? It looks like a useful
feature.
 
A

Alf P. Steinbach

* Szabolcs:
Is it considered for the next version of C++?
No.


It looks like a useful feature.

The /functionality/ is, but not the specific implementation.

For the functionality platform-specific solutions such as _alloca exist
(useful especially for converting strings).

A C VLA corresponds to some templated type in C++. The reason language
support would be nice is that _alloca (or whatever) needs to be executed
at top level in the scope where the memory is to be allocated. Thus, to
emulate C VLAs in C++ you need to use evil macros: you can't just put an
_alloca call in a constructor of some templated type.


Cheers, & hth.,

- Alf
 
J

James Kanze

Is the following legal?
void fun(int N) {
int arr[N];
}
gcc and Digital Mars accept it, but msvc complains that it
wants a constant expression.

Have you invoked gcc as a C++ compiler (g++ -std=c++98
-pedantic), or as a C compiler (or as a C++ compiler with
extensions)? I get an error with g++ 4.1.0.

Anyway, it's legal C, but in C++, we'd write:

void
fun( int N )
{
std::vector< int > arr( N ) ;
}
 
J

James Kanze

Szabolcs said:
Is the following legal?
void fun(int N) {
int arr[N];
}
gcc and Digital Mars accept it, but msvc complains that it wants a
constant expression.
Not currently. Some compilers, as explained, use it as an
extention. It is my understanding, however, that the next
version of C++ will allow it.

I don't think so. VLA's, as they are know as in C, are more
complicated than that, and can appear in a number of contexts,
some of which cause real problems with the C++ object model.
Adopting the C version into C++ would require a significant
amount of work, and given the fact that C++ already has more
flexible (and better designed) alternatives, nobody was
motivated to do that work.
 
J

Joe Greer

Is the following legal?
void fun(int N) {
int arr[N];
}
gcc and Digital Mars accept it, but msvc complains that it
wants a constant expression.

Have you invoked gcc as a C++ compiler (g++ -std=c++98
-pedantic), or as a C compiler (or as a C++ compiler with
extensions)? I get an error with g++ 4.1.0.

Anyway, it's legal C, but in C++, we'd write:

void
fun( int N )
{
std::vector< int > arr( N ) ;
}

Of course, they aren't exactly the same since (as I understand it)

int arr[N];

is totally on the stack whereas

std::vector<int> arr(N);

has a small piece on the stack and the array itself is on the heap.
Normally this isn't a problem, but it is a difference.

joe
 
S

Szabolcs

James said:
VLA's, as they are know as in C, are more
complicated than that, and can appear in a number of contexts,
some of which cause real problems with the C++ object model.

Could you please explain exactly how are VLAs problematic in C++? (I am
just curious about this.)

Szabolcs
 
G

Greg Comeau

James Kanze said:
Is the following legal?
void fun(int N) {
int arr[N];
}
gcc and Digital Mars accept it, but msvc complains that it
wants a constant expression.

Have you invoked gcc as a C++ compiler (g++ -std=c++98
-pedantic), or as a C compiler (or as a C++ compiler with
extensions)? I get an error with g++ 4.1.0.

Anyway, it's legal C, but in C++, we'd write:

void
fun( int N )
{
std::vector< int > arr( N ) ;
}

Of course, they aren't exactly the same since (as I understand it)

int arr[N];

is totally on the stack whereas

std::vector<int> arr(N);

has a small piece on the stack and the array itself is on the heap.
Normally this isn't a problem, but it is a difference.

Although VLAs might normally be done on the stack,
there is no such requirement. For instance, Comeau C99 allows
for a stack model and a heap model to exist, depending upon
the implementation and options used, etc.
 
G

Greg Comeau

Could you please explain exactly how are VLAs problematic in C++? (I am
just curious about this.)

C does not have to worry about things like destructors and
exception handling, so the normal facility provided by suggested
syntax has a new set of kinks to deal with.
 
R

red floyd

Alf said:
* Szabolcs:

The /functionality/ is, but not the specific implementation.

For the functionality platform-specific solutions such as _alloca exist
(useful especially for converting strings).

A C VLA corresponds to some templated type in C++. The reason language
support would be nice is that _alloca (or whatever) needs to be executed
at top level in the scope where the memory is to be allocated. Thus, to
emulate C VLAs in C++ you need to use evil macros: you can't just put an
_alloca call in a constructor of some templated type.

Besides, in C++, instead of a VLA, you can use

void f(int N)
{
std::vector<int> a(N);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top