By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Variables for array size

P: n/a

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?
Mar 17 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
* 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<typearr( x );
...
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 17 '07 #2

P: n/a

"barcaroller" <ba*********@music.netwrote in message
news:et**********@aioe.org...
>
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<Tarr(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
Mar 17 '07 #3

P: n/a
On 3月17日, 上午8时39分, "barcaroller" <barcarol...@music.netwrote:
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!

Mar 17 '07 #4

P: n/a
On Sat, 17 Mar 2007 01:40:29 +0100, "Alf P. Steinbach"
<al***@start.nowrote in comp.lang.c++:
* 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.
In standard C++ do

foo( int x )
{
std::vector<typearr( x );
...
}
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 17 '07 #5

P: n/a
Dnia Sat, 17 Mar 2007 01:26:16 +0000, Mike Wahler napisa(a):
>Can variables be used for array size in C++?

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];
}
--
SasQ
Mar 17 '07 #6

P: n/a
SasQ wrote:
Dnia Sat, 17 Mar 2007 01:26:16 +0000, Mike Wahler napisa(a):
>>Can variables be used for array size in C++?

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
Mar 17 '07 #7

P: n/a
SasQ wrote:
Dnia Sat, 17 Mar 2007 01:26:16 +0000, Mike Wahler napisa?(a):
>>Can variables be used for array size in C++?

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
Mar 17 '07 #8

P: n/a
SasQ wrote:
Dnia Sat, 17 Mar 2007 01:26:16 +0000, Mike Wahler napisa(a):
>>Can variables be used for array size in C++?

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];
}
'x' is not a constant expression. It's not a compile-time const.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 17 '07 #9

P: n/a
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.

--
SasQ
Mar 17 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.