473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Variables for array size


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
9 2140
* 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

"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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Larry L | last post by:
I have a Module that declares several arrays as public, some string, some integers. I then have 2 forms. Form A is the main form, that loads on start-up, and has a command button to open Form B. On...
12
by: zig | last post by:
I've posted this on alt.comp.lang.coldfusion, but is predominantly a javascript problem: I have a CF query which returns several rows of records. I wanted to have a checkbox on each record of...
13
by: Ayaz Ahmed Khan | last post by:
Can a variable containing, say, an integer value be used as size declarator for an array of integers? g++ does not complain, but other compilers, such as Visual C++/6.0, Borland Builder C++, Turbo...
4
by: Deniz Bahar | last post by:
Hello all, Often times programs in C have arrays used as buffers and shared among different sections of code. The need arises to have position indicators to point to different parts of an array...
3
by: jimmygoogle | last post by:
I posted earlier with a scope problem. I think I resolved it in IE but in Firefox it still exists. Anyone have any ideas/experience with this? I attached my code sorry it is so long. You can...
39
by: Gaijinco | last post by:
I have always felt that you should only declared variables as needed, and implicitily it seems many authors to encourage it, but the other day a friend told me that declaring variables inside a...
2
by: justplain.kzn | last post by:
Hi, I have a table with dynamic html that contains drop down select lists and readonly text boxes. Dynamic calculations are done on change of a value in one of the drop down select lists. ...
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
3
bilibytes
by: bilibytes | last post by:
Hi everyone, I am wondering if it is possible to extract the key values of an array into object variables. lets say i have the following $_REQUEST array: $var_array = array("color" =>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.