473,406 Members | 2,894 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,406 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 3114
"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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.