473,394 Members | 1,703 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,394 software developers and data experts.

int a, int b[a]

Kev
If I understand correctly, this should not work:

int a;
int b[a];

An SDK file I am checking out uses this in 3 or 4 structs. It does say its
written in C style. If that makes a difference.

Also is this right? The read() function in <fstream.h> uses an unsigned
char. But <fstream> uses char?
Sep 22 '05 #1
17 1980
Kev
Kev <di**************@pht.zzz> wrote in
news:Xn**********************@216.168.3.44:
If I understand correctly, this should not work:

int a;
int b[a];

An SDK file I am checking out uses this in 3 or 4 structs. It does say
its written in C style. If that makes a difference.


Note: seems for the first part those particular structs (its in a spec file
btw) are more for what information they should contain. Not so much how to
make them work. Eg: finding a, then using new to create b.
Sep 22 '05 #2
Kev schreef:
The read() function in <fstream.h> uses an unsigned char.
But <fstream> uses char?


fstream.h is not a standard header. It can contain anything. Typically
it's included by compilers to provide support for pre-standard programs
(i.e. before 1998). Since it's pre-standard, we can't say anything
sensible about what's in it.

HTH,
Michiel Salters

Sep 22 '05 #3


Kev wrote:
If I understand correctly, this should not work:

int a;
int b[a];
AFAIK, that is a memory allocation fault waiting to happen.
An SDK file I am checking out uses this in 3 or 4 structs. It does say its
written in C style. If that makes a difference.

There may be some deeper cleverness in the structs themselves, but from
the code posted it looks like an error.

Also is this right? The read() function in <fstream.h> uses an unsigned
char. But <fstream> uses char?


Sep 22 '05 #4

Kev wrote:
If I understand correctly, this should not work:

int a;
int b[a];
You are correct - this does not work.
An SDK file I am checking out uses this in 3 or 4 structs. It does say its
written in C style. If that makes a difference.
Okay. "C style" implies that it is not C. I doubt any C/C++-compiler
supports that syntax - even as an extension. It is not C and not C++.
C99 does support a syntax like that (so far as I know - I'm not a C99
expert), but only as part of a function-local parameter.

Also is this right? The read() function in <fstream.h> uses an unsigned
char. But <fstream> uses char?


Could be. I don't know fstream.

/Peter

Sep 22 '05 #5
> Kev wrote:
If I understand correctly, this should not work:

int a;
int b[a];

AFAIK, that is a memory allocation fault waiting to happen.


No, it is not. I don't see a chance the code gets compiled.
Sep 22 '05 #6
This should fail compilation for both C and C++
-Abhi

Sep 22 '05 #7

"Kev" <di**************@pht.zzz> wrote in message
news:Xn**********************@216.168.3.44...
If I understand correctly, this should not work:

int a;
int b[a];

An SDK file I am checking out uses this in 3 or 4 structs. It does say its
written in C style. If that makes a difference.


What does it _really_ look like? The above can't possibly compile, for two
reasons. First, the size of the array has to be a compile-time constant,
not a variable. Second, the code you've shown doesn't even initialize a to
anything, so there's no way in the world for the compiler to know how big to
make the array, even if it were _allowed_ to use plain int variables for the
array size.

Perhaps that was more like this?

const int a = 3;
int b[a];

?

-Howard

Sep 22 '05 #8
AFAIK, variable length arrays are supported by C99. I've used similar
constuctions and they compile (with recent gcc versions) and run just
fine. E.g., the following program does exactly what it should (gcc
3.2.3). Surprised? So was I when I first saw it work. Is this a good
idea for a cross platform code? Probably not - haven't tested it yet
with other compilers.

#include <iostream>
using namespace std;

int main(void)
{
unsigned int a;
cin >> a;

int b[a];

for (int i = 0; i < a; i++)
b[i] = i;

for (int i = 0; i < a; i++)
cerr << b[i] << endl;

}

Sep 22 '05 #9
Sudarshan Raghunathan wrote:
AFAIK, variable length arrays are supported by C99. I've used similar
constuctions and they compile (with recent gcc versions) and run just
fine. E.g., the following program does exactly what it should (gcc
3.2.3). Surprised? So was I when I first saw it work. Is this a good
idea for a cross platform code? Probably not - haven't tested it yet
with other compilers.

#include <iostream>
using namespace std;
Above you mentioned C99, but here you are using C++ stuff.
int main(void)
{
unsigned int a;
cin >> a;

int b[a];
Won't compile. Needs a compile-time constant expression.
for (int i = 0; i < a; i++)
b[i] = i;
Signed/unsigned mismatch warning.
for (int i = 0; i < a; i++)
cerr << b[i] << endl;


Signed/unsigned mismatch warning.
Socks

Sep 22 '05 #10

"Sudarshan Raghunathan" <rd******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
AFAIK, variable length arrays are supported by C99. I've used similar
constuctions and they compile (with recent gcc versions) and run just
fine. E.g., the following program does exactly what it should (gcc
3.2.3). Surprised? So was I when I first saw it work. Is this a good
idea for a cross platform code? Probably not - haven't tested it yet
with other compilers.

#include <iostream>
using namespace std;

int main(void)
{
unsigned int a;
cin >> a;

int b[a];


In C++, the array size _must_ be a compile-time constant. If you're
compiling as a C++ program, and your compiler allows this, then it's either
a non-standard extension provided by that compiler, or a non-standard
compiler.

-Howard

Sep 22 '05 #11
I suspect it is the former (non-standard extension provided by gcc)
rather than the latter (I'm not using an obscure C++ compiler on an
obscure platform). My only point was that you can get away with such
constructs with certain versions of gcc -nothing else.

Sep 22 '05 #12
Howard wrote:
"Sudarshan Raghunathan" <rd******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
AFAIK, variable length arrays are supported by C99. I've used similar
constuctions and they compile (with recent gcc versions) and run just
fine. E.g., the following program does exactly what it should (gcc
3.2.3). Surprised? So was I when I first saw it work. Is this a good
idea for a cross platform code? Probably not - haven't tested it yet
with other compilers.

#include <iostream>
using namespace std;

int main(void)
{
unsigned int a;
cin >> a;

int b[a];

In C++, the array size _must_ be a compile-time constant. If you're
compiling as a C++ program, and your compiler allows this, then it's either
a non-standard extension provided by that compiler, or a non-standard
compiler.

-Howard

o, amazing. I copied that code and compile. It really went through
successfully! I couldn't believe it but you may try it too. I am using
gcc 3.3.3.
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --disable-libunwind-exceptions --with-system-zlib
--enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)

-X
Sep 22 '05 #13
Oh, and here's some documentation too (just search for variable length
arrays):
http://developer.apple.com/documenta...le-Length.html

Sep 22 '05 #14
Kev
"Howard" <al*****@hotmail.com> wrote in
news:Pi*******************@bgtnsc04-news.ops.worldnet.att.net:
What does it _really_ look like? The above can't possibly compile,
for two reasons. First, the size of the array has to be a
compile-time constant, not a variable. Second, the code you've shown
doesn't even initialize a to anything, so there's no way in the world
for the compiler to know how big to make the array, even if it were
_allowed_ to use plain int variables for the array size.

Perhaps that was more like this?

const int a = 3;
int b[a];


Quite right. It doesnt compile. Really I didnt expect it to. But it did
make me wonder its even there. It was suggested to me last evening that its
merely to show what information in contains, not how to use it. And that in
this case 'triangleIndices[]' must be allocated dynamically. So I suppose I
was misinterpreting what it was.

Its from the Milkshape 3D specfile btw. Arrays in the others are declared
the usual way. "myarray[10]".

typedef struct
{
byte flags;
char name[32];
word numtriangles;
word triangleIndices[numtriangles];
char materialIndex;
} ms3d_group_t;
Sep 22 '05 #15

"Kev" <di**************@pht.zzz> wrote in message
typedef struct
{
byte flags;
char name[32];
word numtriangles;
word triangleIndices[numtriangles];
char materialIndex;
} ms3d_group_t;


I think that you're looking at a C example, not a C++ example. That form of
declaring a struct via a typedef statement indicates to me that this is C
code. It's not legal C++, even if some compiler does accept it.

I can't personally verify one way or the other if it's legal in C, though.

(By the way, in C++, I'd prefer to use std::vector over a raw array,
whenever possible.)

-Howard


Sep 22 '05 #16
Kev
"Howard" <al*****@hotmail.com> wrote in
news:Uv*******************@bgtnsc04-news.ops.worldnet.att.net:
I think that you're looking at a C example, not a C++ example. That
form of declaring a struct via a typedef statement indicates to me
that this is C code. It's not legal C++, even if some compiler does
accept it.


Yes it says its written in C style.
Sep 22 '05 #17
xuatla wrote:
"Sudarshan Raghunathan" <rd******@gmail.com> wrote:
#include <iostream>
using namespace std;

int main(void)
{
unsigned int a;
cin >> a;

int b[a];


o, amazing. I copied that code and compile. It really went through
successfully! I couldn't believe it but you may try it too. I am using
gcc 3.3.3.


GCC will give an error iff you specify the -pedantic switch.
In fact, for checking that your code conforms to ISO C++, you
should compile with at least these switches:

g++ -Wall -Wextra -ansi -pedantic

Sep 23 '05 #18

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

Similar topics

0
by: ba-admin | last post by:
Your mail to 'Ba' with the subject Re: Incoming Fax Is being held until the list moderator can review it for approval. The reason it is being held: Post by non-member to a members-only...
8
by: Dmitri Shvetsov | last post by:
Hi All, Did somebody try to get the Hash code from byte array? The method exists, the program is compilable but...))) Try!-) Every time you can get the different code from the same array, and...
5
by: Wysiwyg | last post by:
I'm new to c# programming and can't figure out how to avoid duplicating common code in multiple classes when I'm restricted to using different system base classes.. I'm using c# in asp.net to write...
0
by: Jaime Stuardo | last post by:
Hi all... I have the following curious problem, I have a GridView that I bind after I press a button in the web form. That grid has a template column, which contains a checkbox (in the header...
3
by: WT | last post by:
Hello, I am trying to reset the content of mainy gridviews when some line is selected. All these gridviews have viewstate enabled Using C# and asp.net, I try changing the datasource to null in...
12
by: rwise5 | last post by:
Hello, I was wondering if someone could help me with the following? Write a function that shifts the stored value of five character variables in a circular fashion. Your function should work in...
3
by: jrett | last post by:
I've got a source tree with over 100 projects, some depending on others and I'd like to set this up on an automated nightly build, or where devs can do private builds of the entire source tree to...
0
by: ali | last post by:
ba sabtenam raygan dar in site hoghogh mahane daryaft konid http://al1358i.blogfa.com/
0
by: Vijay | last post by:
Prep Courses for International Certifications, CSTE & CSQA & ISEB & ISTQB &Business Analyst & SOA Certifications in HYDERABAD. After receiving overwhelming response to our last 50+ batches, ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.