473,324 Members | 2,268 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,324 software developers and data experts.

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.

Jul 22 '05 #1
13 3112
"Ayaz Ahmed Khan" <re*******@myrealbox.com> wrote in message
news:pa*********************************@myrealbox .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.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #2
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

Jul 22 '05 #3
Jakob Bieling wrote:

"Ayaz Ahmed Khan" <re*******@myrealbox.com> wrote in message
news:pa*********************************@myrealbox .com...
Can a variable containing, say, an integer value be used as size
declarator for an array of integers?

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
Jul 22 '05 #4
"Jakob Bieling" typed:
"Ayaz Ahmed Khan" <re*******@myrealbox.com> wrote in message
news:pa*********************************@myrealbox .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/...34830malz.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.

Jul 22 '05 #5
"Rolf Magnus" typed:
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.

Jul 22 '05 #6
Ayaz Ahmed Khan wrote in
news:pa*********************************@myrealbox .com:
"Jakob Bieling" typed:
"Ayaz Ahmed Khan" <re*******@myrealbox.com> wrote in message
news:pa*********************************@myrealbox .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.
Note:

http://www.cray.com/craydoc/manuals/...-2179-003/z893
434830malz.html


The page title says "Chapter 3. Cray C and C++ Extensions".
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7
In gnu.g++.help Ayaz Ahmed Khan <re*******@myrealbox.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.

Andre'
Jul 22 '05 #8
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 <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.

Jul 22 '05 #9
"Rolf Magnus" typed:
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 <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.

Jul 22 '05 #10
"André Pönitz" typed:
In gnu.g++.help Ayaz Ahmed Khan <re*******@myrealbox.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.

Jul 22 '05 #11
"Rob Williscroft" typed:
Ayaz Ahmed Khan wrote in
news:pa*********************************@myrealbox .com:
"Jakob Bieling" typed:
"Ayaz Ahmed Khan" <re*******@myrealbox.com> wrote in message
news:pa*********************************@myrealbox .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?
Note:

http://www.cray.com/craydoc/manuals/...-2179-003/z893
434830malz.html

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.

Jul 22 '05 #12
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #13
"Rob Williscroft" typed:
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."

Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
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...
3
by: lovecreatesbeauty | last post by:
It is strange. Happen to get following the fragment compiled successfully. Is there anything wrong with my compiler or my mind? Please guide me to correct my understanding on it. Sincerely, ...
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. ...
18
by: mdh | last post by:
>From p112 ( K&R). Given an array declared as static char arr= { { 0,1,........},{0,1,.....}}; let arr be passed as an argument to f. f( int (*arr) ) {....} It is noted that the...
5
by: borophyll | last post by:
I have two questions. The first: does the declaration of an array parameter in a function prototype which uses the notation mean that the array must be a variable length array. That is, the...
11
by: lak | last post by:
Hello firends. I want to know what is the variable length in c. I K&R they stated that atleast 31 character. But I give 1 lakhs length to a variable, but my compiler doesn't say any error. It...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.