Variables as array size declarators.

A

Ayaz Ahmed Khan

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.
 
J

Jakob Bieling

Ayaz Ahmed Khan said:
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
 
R

Rolf Magnus

Ayaz said:
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
 
D

Default User

Jakob said:
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
 
A

Ayaz Ahmed Khan

"André Pönitz" typed:
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.
 
A

Ayaz Ahmed Khan

"Rolf Magnus" typed:
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.


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.
 
A

Ayaz Ahmed Khan

"Rob Williscroft" typed:
Ayaz Ahmed Khan wrote in
"Jakob Bieling" typed:
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?


The page title says "Chapter 3. Cray C and C++ Extensions".


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.
 
R

Rob Williscroft

Ayaz Ahmed Khan wrote in @myrealbox.com:
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.
 
A

Ayaz Ahmed Khan

"Rob Williscroft" typed:
Ayaz Ahmed Khan wrote in @myrealbox.com:
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."
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top