 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
HappyHippy Guest
|
Posted: Wed Oct 12, 2005 4:31 pm Post subject: variable as an array size |
|
|
Hi,
I'm wondering what you think about this piece of code:
#include<iostream>
int main()
{
int size;
std::cin >> size;
int array[size];
std::cout<
return 0;
}
Is it legal or not (according to C/C++ standard)?
A couple of days ago I would say it is not, only constant expression can
be used to specify the size, of the array, but now I'm not sure The
reason is that gcc 3.4.4 compiles at with no errors and warnings.
And the result printed by the program is correct:
sizeof(arr) = size * sizeof(int)
P.S.
MS Visual C++ 7.1 does not compile this code. It gives the following errors:
main.cpp(7) : error C2057: expected constant expression
main.cpp(7) : error C2466: cannot allocate an array of constant size 0
main.cpp(7) : error C2133: 'array' : unknown size
main.cpp(9) : error C2070: 'int []': illegal sizeof operand
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Wed Oct 12, 2005 10:47 pm Post subject: Re: variable as an array size |
|
|
HappyHippy <karenman (AT) mail (DOT) ru> writes:
This declares std::cin and std::cout, but not all the required
operator<< overloads. #include
To err on the safe side, I also #include <istream> if I call an
operator>> (the one your code uses is a member of basic_istream, so
that wouldn't strictly be necessary here).
| Quote: | int main()
{
int size;
std::cin >> size;
int array[size];
|
I don't think much of this, but not for the reason you expect:
The input operation can fail. If that happens, size is left
unitialized. Reading an uninitialized variable has undefined behavior.
As for your real question, variably sized arrays were added to C in
1999. The ISO C++ Standard is based on C 90, though.
| Quote: | std::cout<
return 0;
}
Is it legal or not (according to C/C++ standard)?
|
There is no such thing as a "C/C++ standard".
The program isn't legal according to the C Standard, but only because
it uses components of the C++ Standard Library not present in the C
Standard Library.
The problem isn't legal according to the ISO C++ Standard because it
doesn't (yet) support variably sized arrays. You can use std::vector
instead.
| Quote: | A couple of days ago I would say it is not, only constant expression can
be used to specify the size, of the array, but now I'm not sure The
reason is that gcc 3.4.4 compiles at with no errors and warnings.
|
That's an implementation-specific extension then. One that makes a lot
of sense given the fact that gcc compiles both C and C++.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Wed Oct 12, 2005 10:50 pm Post subject: Re: variable as an array size |
|
|
HappyHippy wrote:
| Quote: | Hi,
I'm wondering what you think about this piece of code:
#include<iostream
int main()
{
int size;
std::cin >> size;
int array[size];
std::cout<
return 0;
}
Is it legal or not (according to C/C++ standard)?
|
It is illegal according to the current C++ standard. g++ compiles this
because it is allowed by C99, although it is not a part of the C++
standard. g++ does not compile the same code with -pedantic.
--
Valentin Samko - http://val.samko.info
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Thu Oct 13, 2005 1:14 pm Post subject: Re: variable as an array size |
|
|
HappyHippy wrote:
| Quote: | int main()
{
int size;
std::cin >> size;
int array[size];
std::cout<
return 0;
}
Is it legal or not (according to C/C++ standard)?
|
There is no C/C++ standard! C and C++ are two different languages
covered by two different standards. In fact, the "variable length array"
feature is allowed in C (to be precise in C99), but not in C++.
| Quote: | A couple of days ago I would say it is not, only constant expression can
be used to specify the size, of the array, but now I'm not sure The
reason is that gcc 3.4.4 compiles at with no errors and warnings.
|
AFAIK gcc is implementing C99 quite well, so it's no surprise that it
supports variable length arrays in C programs. Apparently, gcc is also
is allowing this C-only feature even in C++ programs. This has to be
regarded as a non-standard extension, however. Other compilers are not
required to do the same. In case you are wondering, I didn't get signals
about a possible inclusion of variable length arrays in C++, mainly
because the object model (read: construction and destruction of UDTs) is
rather different from C. Of course I might have missed some discussion.
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|