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. 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)
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
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
"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.
"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.
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/
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'
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.
"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.
"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.
"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.
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/
"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." This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;
};
|
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...
|
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...
|
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...
|
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,
...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
|
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...
| |