Variables for array size

B

barcaroller

Can variables be used for array size in C++? I know that in the past, I
could not do the following:

foo (int x)
{
type arr[x];
}

I have recently seen code that does exactly that. Is it right?
 
A

Alf P. Steinbach

* barcaroller:
Can variables be used for array size in C++? I know that in the past, I
could not do the following:

foo (int x)
{
type arr[x];
}

I have recently seen code that does exactly that. Is it right?

Not in standard C++, but g++ offers it as an extension (based on C99).

In standard C++ do

foo( int x )
{
std::vector<type> arr( x );
...
}
 
M

Mike Wahler

barcaroller said:
Can variables be used for array size in C++?

Not with standard C++. The specified array dimension
must be a constant expression.
I know that in the past, I could not do the following:

foo (int x)
{
type arr[x];
}

I have recently seen code that does exactly that. Is it right?

Depends upon what you mean by 'right'. It's not correct
for standard C++, however I believe there are implementations
which support this as an extension. Such extensions are not
topical here.

If you want to specify the size of collection of objects at
run time, the tool to use is a std::vector.

#include <vector>

template <typename T>
void foo(std::vector<T>::size_type x)
{
std::vector<T> arr(x);
}


This requires that type 'T' is either a built-in type
(all members will be zero-initialized), or that type 'T'
provides a default constructor (all members will be default-
initialized).

-Mike
 
D

dolphin

Can variables be used for array size in C++? I know that in the past, I
could not do the following:

foo (int x)
{
type arr[x];

}

I have recently seen code that does exactly that. Is it right?

May be you should not do this.But STL may be a good choice for you!
 
J

Jack Klein

* barcaroller:
Can variables be used for array size in C++? I know that in the past, I
could not do the following:

foo (int x)
{
type arr[x];
}

I have recently seen code that does exactly that. Is it right?

Not in standard C++, but g++ offers it as an extension (based on C99).

gcc and g++ do provide this as a non-standard extension, but it is
most certainly not based on C99, as they offered it long before there
was a C99.
 
K

Kai-Uwe Bux

SasQ said:
Dnia Sat, 17 Mar 2007 01:26:16 +0000, Mike Wahler napisa?(a):
Not with standard C++. The specified array dimension
must be a constant expression.

Hmm.. so what about this?
foo (const int x)
{
type arr[x];
}

That x is not a constant expression [5.19/1]:

In several places, C + + requires expressions that evaluate to an integral
or enumeration constant: as array bounds (8.3.4, 5.3.4), as case
expressions (6.4.2), as bit-field lengths (9.6), as enumerator
initializers (7.2), as static member initializers (9.4.2), and as integral
or enumeration non-type template arguments (14.3).

constant-expression:
conditional-expression

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), non-type
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. Only type conversions to integral or
enumeration types can be used. In particular, except in sizeof
expressions, functions, class objects, pointers, or references shall not
be used, and assignment, increment, decrement, function-call, or comma
operators shall not be used.

In particular, note that const variables only qualify as constant
expressions if they are initialized from another constant expression.


Best

Kai-Uwe Bux
 
B

Bo Persson

SasQ said:
Dnia Sat, 17 Mar 2007 01:26:16 +0000, Mike Wahler napisa³(a):
Not with standard C++. The specified array dimension
must be a constant expression.

Hmm.. so what about this?
foo (const int x)
{
type arr[x];
}

No. The value of x is constant inside the function, at run-time. The
declaration works for C99, but not for C++.

In C++, a "constant expression" is a technical term for something that can
be evaluated at compile-time.


Bo Persson
 
S

SasQ

Dnia Sat, 17 Mar 2007 15:27:25 +0100, Bo Persson napisa³(a):
Hmm.. so what about this?

foo (const int x)
{
type arr[x];
}

No. The value of x is constant inside the function, at run-time.
The declaration works for C99, but not for C++.

I know that, but I wanted to focus OP's attention that not every
constant expression can be used as an array size. Only the
integral constant expressions, and only that their value could be
known already at the compilation time.
 

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,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top