 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ayaz Ahmed Khan Guest
|
Posted: Thu Feb 26, 2004 4:28 pm Post subject: Variables as array size declarators. |
|
|
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 C++, and
Dev-C++/4.0, that I tested it on decline to use variables as size
declarators.
--
Ayaz Ahmed Khan
Yours Forever in, | Webmaster,
Cyberspace. | http://fast-ce.org/
_______________________________________________
I ditched Windows for Linux over two years ago.
Life's never been better since.
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Thu Feb 26, 2004 4:30 pm Post subject: Re: Variables as array size declarators. |
|
|
"Ayaz Ahmed Khan" <resilient (AT) myrealbox (DOT) com> wrote
| Quote: | 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 C++, and
Dev-C++/4.0, that I tested it on decline to use variables as size
declarators.
|
Not sure, but I think C allows this and g++ allows it as an extension.
However, it is not part of Standard C++. If you want to use this kind of
stuff, use the std::vector container class. It will do exactly what you
want.
hth
--
jb
(replace y with x if you want to reply by e-mail)
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Feb 26, 2004 4:35 pm Post subject: Re: Variables as array size declarators. |
|
|
Ayaz Ahmed Khan wrote:
| Quote: | Can a variable containing, say, an integer value be used as size
declarator for an array of integers?
|
No.
| Quote: | g++ does not complain, but other compilers, such as Visual C++/6.0,
Borland Builder C++, Turbo C++, and Dev-C++/4.0, that I tested it on
decline to use variables as size declarators.
|
You probably didn't use g++ in ansi/iso mode. Try to use at least the
following command line switches:
-ansi -pedantic -W -Wall
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Thu Feb 26, 2004 7:22 pm Post subject: Re: Variables as array size declarators. |
|
|
Jakob Bieling wrote:
| Quote: |
"Ayaz Ahmed Khan" <resilient (AT) myrealbox (DOT) com> wrote in message
news:pan.2004.02.26.15.51.50.745504.1849 (AT) myrealbox (DOT) com...
Can a variable containing, say, an integer value be used as size
declarator for an array of integers?
|
| Quote: | Not sure, but I think C allows this and g++ allows it as an extension.
However, it is not part of Standard C++. If you want to use this kind of
stuff, use the std::vector container class. It will do exactly what you
want.
|
These (variable-length arrays) were added to C in the latest standard.
As there are few compilers actually implementing that standard yet,
actually having that capability in a standard-compliant way is spotty.
Brian Rodenborn
|
|
| Back to top |
|
 |
Ayaz Ahmed Khan Guest
|
Posted: Fri Feb 27, 2004 4:54 am Post subject: Re: Variables as array size declarators. |
|
|
"Jakob Bieling" typed:
| Quote: | "Ayaz Ahmed Khan" <resilient (AT) myrealbox (DOT) com> wrote in message
news:pan.2004.02.26.15.51.50.745504.1849 (AT) myrealbox (DOT) com...
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
C++, and Dev-C++/4.0, that I tested it on decline to use variables
as size declarators.
Not sure, but I think C allows this and g++ allows it as an
extension.
However, it is not part of Standard C++. If you want to use this
kind of stuff, use the std::vector container class. It will do
exactly what you want.
|
I had been under the impression that it was a feature incorporated
into standard C++. I found a reference to VLAs[1], but am not sure what
it, actually, is worth.
Note:
http://www.cray.com/craydoc/manuals/004-2179-003/html-004-2179-003/z893434830malz.html
--
Ayaz Ahmed Khan
Yours Forever in, | Webmaster,
Cyberspace. | http://fast-ce.org/
_______________________________________________
I ditched Windows for Linux over two years ago.
Life's never been better since.
|
|
| Back to top |
|
 |
Ayaz Ahmed Khan Guest
|
Posted: Fri Feb 27, 2004 4:54 am Post subject: Re: Variables as array size declarators. |
|
|
"Rolf Magnus" typed:
| Quote: | Ayaz Ahmed Khan wrote:
Can a variable containing, say, an integer value be used as size
declarator for an array of integers?
No.
g++ does not complain, but other compilers, such as Visual C++/6.0,
Borland Builder C++, Turbo C++, and Dev-C++/4.0, that I tested it
on decline to use variables as size declarators.
You probably didn't use g++ in ansi/iso mode. Try to use at least
the following command line switches:
-ansi -pedantic -W -Wall
|
Yes. It now complains when I use the above switches. But if I wasn't
using g++ in ANSI/ISO mode before, why did it flag with an "ISO for
scope" message, or something to that effect, an identifier declared
in the for header that I tried to use elsewhere in the listing?
--
Ayaz Ahmed Khan
Yours Forever in, | Webmaster,
Cyberspace. | http://fast-ce.org/
_______________________________________________
I ditched Windows for Linux over two years ago.
Life's never been better since.
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Fri Feb 27, 2004 5:16 am Post subject: Re: Variables as array size declarators. |
|
|
Ayaz Ahmed Khan wrote in
news:pan.2004.02.27.04.40.24.376057.7652 (AT) myrealbox (DOT) com:
| Quote: | "Jakob Bieling" typed:
"Ayaz Ahmed Khan" <resilient (AT) myrealbox (DOT) com> wrote in message
news:pan.2004.02.26.15.51.50.745504.1849 (AT) myrealbox (DOT) com...
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
C++, and Dev-C++/4.0, that I tested it on decline to use variables
as size declarators.
Not sure, but I think C allows this and g++ allows it as an
extension.
However, it is not part of Standard C++. If you want to use this
kind of stuff, use the std::vector container class. It will do
exactly what you want.
I had been under the impression that it was a feature incorporated
into standard C++. I found a reference to VLAs[1], but am not sure
what it, actually, is worth.
|
VLA's are a feature of Standard C, C++ compilers that support them
(eg: g++) do so as an extension to Standard C++. IOW using VLA's in
C++ is *not* portable.
The page title says "Chapter 3. Cray C and C++ Extensions".
Rob.
--
http://www.victim-prime.dsl.pipex.com/
|
|
| Back to top |
|
 |
André Pönitz Guest
|
Posted: Fri Feb 27, 2004 9:36 am Post subject: Re: Variables as array size declarators. |
|
|
In gnu.g++.help Ayaz Ahmed Khan <resilient (AT) myrealbox (DOT) com> wrote:
| Quote: | I had been under the impression that it was a feature incorporated
into standard C++.
|
It's not in the current C++ Standard (1998) but in C (1999).
I wouldn't use it in my code.
Andre'
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Feb 27, 2004 10:04 am Post subject: Re: Variables as array size declarators. |
|
|
Ayaz Ahmed Khan wrote:
| Quote: | g++ does not complain, but other compilers, such as Visual C++/6.0,
Borland Builder C++, Turbo C++, and Dev-C++/4.0, that I tested it
on decline to use variables as size declarators.
You probably didn't use g++ in ansi/iso mode. Try to use at least
the following command line switches:
-ansi -pedantic -W -Wall
Yes. It now complains when I use the above switches. But if I wasn't
using g++ in ANSI/ISO mode before, why did it flag with an "ISO for
scope" message, or something to that effect, an identifier declared
in the for header that I tried to use elsewhere in the listing?
|
I think that g++ gives that error message when you try to use a variable
that was defined in a for loop after that loop. So e.g. the following
erroneous code would produce that:
#include <iostream>
int main()
{
for (int i = 0; i < 10; ++i)
std::cout << i << 'n';
i = 3; // error, i is not defined after the for loop
}
In some old C++ dialects before ISO, that was allowed, and i was defined
up until the end of the enclosing block. VC++ is known to still handle
this incorrectly.
|
|
| Back to top |
|
 |
Ayaz Ahmed Khan Guest
|
Posted: Sat Feb 28, 2004 6:23 pm Post subject: Re: Variables as array size declarators. |
|
|
"André Pönitz" typed:
| Quote: | In gnu.g++.help Ayaz Ahmed Khan <resilient (AT) myrealbox (DOT) com> wrote:
I had been under the impression that it was a feature incorporated
into standard C++.
It's not in the current C++ Standard (1998) but in C (1999). I
wouldn't use it in my code.
|
Thanks, Andre.
--
Ayaz Ahmed Khan
Yours Forever in, | Webmaster,
Cyberspace. | http://fast-ce.org/
_______________________________________________
I ditched Windows for Linux over two years ago.
Life's never been better since.
|
|
| Back to top |
|
 |
Ayaz Ahmed Khan Guest
|
Posted: Sat Feb 28, 2004 6:23 pm Post subject: Re: Variables as array size declarators. |
|
|
"Rolf Magnus" typed:
| Quote: | Ayaz Ahmed Khan wrote:
g++ does not complain, but other compilers, such as Visual
C++/6.0, Borland Builder C++, Turbo C++, and Dev-C++/4.0, that I
tested it on decline to use variables as size declarators.
You probably didn't use g++ in ansi/iso mode. Try to use at least
the following command line switches:
-ansi -pedantic -W -Wall
Yes. It now complains when I use the above switches. But if I
wasn't using g++ in ANSI/ISO mode before, why did it flag with an
"ISO for scope" message, or something to that effect, an identifier
declared in the for header that I tried to use elsewhere in the
listing?
I think that g++ gives that error message when you try to use a
variable that was defined in a for loop after that loop. So e.g. the
following erroneous code would produce that:
#include
int main()
{
for (int i = 0; i < 10; ++i)
std::cout << i << 'n';
i = 3; // error, i is not defined after the for loop
}
}
In some old C++ dialects before ISO, that was allowed, and i was
defined up until the end of the enclosing block. VC++ is known to
still handle this incorrectly.
|
Yes. Exactly that error. But the first time I came across it using
g++, I thought that g++ was, by default, running in ANSI/ISO
standards-complaint mode.
Anyway, thanks for your time.
--
Ayaz Ahmed Khan
Yours Forever in, | Webmaster,
Cyberspace. | http://fast-ce.org/
_______________________________________________
I ditched Windows for Linux over two years ago.
Life's never been better since.
|
|
| Back to top |
|
 |
Ayaz Ahmed Khan Guest
|
Posted: Sat Feb 28, 2004 6:23 pm Post subject: Re: Variables as array size declarators. |
|
|
"Rob Williscroft" typed:
| Quote: | Ayaz Ahmed Khan wrote in
news:pan.2004.02.27.04.40.24.376057.7652 (AT) myrealbox (DOT) com:
"Jakob Bieling" typed:
"Ayaz Ahmed Khan" <resilient (AT) myrealbox (DOT) com> wrote in message
news:pan.2004.02.26.15.51.50.745504.1849 (AT) myrealbox (DOT) com...
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 C++, and Dev-C++/4.0, that I tested it on decline to use
variables as size declarators.
Not sure, but I think C allows this and g++ allows it as an
extension.
However, it is not part of Standard C++. If you want to use this
kind of stuff, use the std::vector container class. It will do
exactly what you want.
I had been under the impression that it was a feature incorporated
into standard C++. I found a reference to VLAs[1], but am not sure
what it, actually, is worth.
VLA's are a feature of Standard C, C++ compilers that support them
(eg: g++) do so as an extension to Standard C++. IOW using VLA's in
C++ is *not* portable.
|
So I see. But aren't there advantages in using VLAs that are defined a
certain size, such as provided by the input variables, at run-time,
over other arrays for the same purposes that have to be specified a
size large enough to accomodate the input data, which can so much
as only be guessed anytime before run-time?
Extensions. Perhaps, I didn't read it so thoroughly as I should have
had.
--
Ayaz Ahmed Khan
Yours Forever in, | Webmaster,
Cyberspace. | http://fast-ce.org/
_______________________________________________
I ditched Windows for Linux over two years ago.
Life's never been better since.
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Sat Feb 28, 2004 6:50 pm Post subject: Re: Variables as array size declarators. |
|
|
Ayaz Ahmed Khan wrote in news:pan.2004.02.28.17.55.17.510698.1784
@myrealbox.com:
| Quote: | VLA's are a feature of Standard C, C++ compilers that support them
(eg: g++) do so as an extension to Standard C++. IOW using VLA's in
C++ is *not* portable.
So I see. But aren't there advantages in using VLAs that are defined a
certain size, such as provided by the input variables, at run-time,
over other arrays for the same purposes that have to be specified a
size large enough to accomodate the input data, which can so much
as only be guessed anytime before run-time?
|
Yes, but with C++ we have std::vector<> to solve that problem.
#include <vector>
extern void takes_a_char_buffer( char *buf, std::size_t sz );
int main()
{
std::size_t sz = /* some runtime value */;
std::vector< char > buf( sz );
takes_a_char_buffer( &buf[0], sz );
}
Rob.
--
http://www.victim-prime.dsl.pipex.com/
|
|
| Back to top |
|
 |
Ayaz Ahmed Khan Guest
|
Posted: Sun Feb 29, 2004 7:11 pm Post subject: Re: Variables as array size declarators. |
|
|
"Rob Williscroft" typed:
| Quote: | Ayaz Ahmed Khan wrote in news:pan.2004.02.28.17.55.17.510698.1784
@myrealbox.com:
VLA's are a feature of Standard C, C++ compilers that support them
(eg: g++) do so as an extension to Standard C++. IOW using VLA's
in C++ is *not* portable.
So I see. But aren't there advantages in using VLAs that are
defined a certain size, such as provided by the input variables, at
run-time, over other arrays for the same purposes that have to be
specified a size large enough to accomodate the input data, which
can so much as only be guessed anytime before run-time?
Yes, but with C++ we have std::vector<> to solve that problem.
#include <vector
extern void takes_a_char_buffer( char *buf, std::size_t sz );
int main()
{
std::size_t sz = /* some runtime value */;
std::vector< char > buf( sz );
takes_a_char_buffer( &buf[0], sz );
}
}
Rob.
|
Thanks, Rob. I'm still far behind in studying containers and STL in
C++, having recently started learning "Classes and Objects".
--
Ayaz Ahmed Khan
Yours Forever in, | Webmaster,
Cyberspace. | http://fast-ce.org/
______________________________
"To death, I shall hate thee."
|
|
| 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
|
|