473,399 Members | 4,177 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,399 software developers and data experts.

Array in struct

Hello all,

I'm writing an XML parser, and I'd like to know how portable a
solution is.

When I pass an STag or EmptyElement tag to the application, I
(obviously) need to pass the attribute list somehow. I thought about
storing the list in something like this:

typedef struct _ATTRLIST
{
int numAttr;
ATTR attr[1];
}ATTRLIST, *PATTRLIST;

foo()
{
PATTRLIST pal = 0;
...blah...
if(nAttr > 0)
{
pal = malloc(sizeof(ATTRLIST)+(nAttr-1)*sizeof(ATTR))
...blah...
}
}

Now, what I'm worried about is portability. Is using an 1-sized array
like that a bad move? Does C guarantee that structure members are ordered?

Thankful for any advice.

--
Kind Regards,
Jan Danielsson
Te audire no possum. Musa sapientum fixa est in aure.
Nov 15 '05 #1
7 2230
Jan Danielsson wrote:
I'm writing an XML parser, and I'd like to know how portable a
solution is.

When I pass an STag or EmptyElement tag to the application, I
(obviously) need to pass the attribute list somehow. I thought about
storing the list in something like this:

typedef struct _ATTRLIST
{
int numAttr;
ATTR attr[1];
}ATTRLIST, *PATTRLIST;

foo()
{
PATTRLIST pal = 0;
...blah...
if(nAttr > 0)
{
pal = malloc(sizeof(ATTRLIST)+(nAttr-1)*sizeof(ATTR))
...blah...
}
}

Now, what I'm worried about is portability. Is using an 1-sized array
like that a bad move?
from the FAQ:-
FAQ 2.6 "I came across some code that declared a structure with the
last member an array of one element, and then did some tricky
allocation to make it act like the array had several elements. Is this
legal or portable?"

It seems to be technically illegal but works on all known
implementations... You could pass two parameters a count and an array
of
attributes.

Does C guarantee that structure members are ordered?


yes.

<snip>
--
Nick Keighley

We recommend, rather, that users take advantage of the extensions of
GNU C and disregard the limitations of other compilers. Aside from
certain supercomputers and obsolete small machines, there is less
and less reason ever to use any other C compiler other than for
bootstrapping GNU CC.
(Using and Porting GNU CC)

Nov 15 '05 #2
Jan Danielsson sade:
Hello all,

I'm writing an XML parser, and I'd like to know how portable a
solution is.

When I pass an STag or EmptyElement tag to the application, I
(obviously) need to pass the attribute list somehow. I thought about
storing the list in something like this:

typedef struct _ATTRLIST
{
int numAttr;
ATTR attr[1];
}ATTRLIST, *PATTRLIST;

foo()
{
PATTRLIST pal = 0;
...blah...
if(nAttr > 0)
{
pal = malloc(sizeof(ATTRLIST)+(nAttr-1)*sizeof(ATTR))
...blah...
}
}

Now, what I'm worried about is portability. Is using an 1-sized array
like that a bad move? Does C guarantee that structure members are ordered?

Thankful for any advice.


The c99 standard provides a feature called a flexible array member which
is a portable struct hack.

typedef struct _ATTRLIST
{
int numAttr;
ATTR attr[];
} ATTRLIST, *PATTRLIST;

PATTRLIST p = malloc(sizeof(ATTRLIST) + sizeof(ATTR) * 10);

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Nov 15 '05 #3
Tobias Blomkvist wrote:
The c99 standard provides a feature called a flexible array member which
is a portable struct hack.

typedef struct _ATTRLIST
{
int numAttr;
ATTR attr[];
} ATTRLIST, *PATTRLIST;

PATTRLIST p = malloc(sizeof(ATTRLIST) + sizeof(ATTR) * 10);

Tobias


Nice to know it is actually legal and portable! I've been using this a lot
and never took the time to check if it was an OK construct or if it gave me
the correct size for malloc.

Where in the standard is this defined? I've decided to actually read the
standard but am still in the beginnings of the document...

--
Eric Laberge
Nov 15 '05 #4
Jan Danielsson wrote:
[...]
typedef struct _ATTRLIST
{
int numAttr;
ATTR attr[1];
}ATTRLIST, *PATTRLIST;

foo()
{
PATTRLIST pal = 0;
...blah...
if(nAttr > 0)
{
pal = malloc(sizeof(ATTRLIST)+(nAttr-1)*sizeof(ATTR))
...blah...
}
}

Now, what I'm worried about is portability. Is using an 1-sized array
like that a bad move? Does C guarantee that structure members are ordered?

[...]

As someone else pointed out, the FAQ says it's "technically illegal",
but it "works on all known implementations".

You could get around this, if you want to not worry about it, by using
something like:

typedef struct _ATTRLIST
{
int numAttr;
ATTR *attr;
}
ATTRLIST, *PATTRLIST;

...
pal = malloc(sizeof(ATTRLIST));
if ( pal != NULL )
{
pal->attr = malloc(nAttr*sizeof(*pal->attr));
...

You also have to remember to free pal->attr as well as pal.

On the other hand, I do have to admit that I use the 1-element array
version in numerous places, and have had no problems on all the
various platforms I've ported to.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 15 '05 #5
Eric Laberge sade:
Tobias Blomkvist wrote:

The c99 standard provides a feature called a flexible array member which
is a portable struct hack.

typedef struct _ATTRLIST
{
int numAttr;
ATTR attr[];
} ATTRLIST, *PATTRLIST;

PATTRLIST p = malloc(sizeof(ATTRLIST) + sizeof(ATTR) * 10);

Tobias

Nice to know it is actually legal and portable! I've been using this a lot
and never took the time to check if it was an OK construct or if it gave me
the correct size for malloc.

Where in the standard is this defined? I've decided to actually read the
standard but am still in the beginnings of the document...


Section 6.7.2.1

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Nov 15 '05 #6
Eric Laberge <de********@myrealbox.com> writes:
Tobias Blomkvist wrote:
The c99 standard provides a feature called a flexible array member which
is a portable struct hack.

typedef struct _ATTRLIST
{
int numAttr;
ATTR attr[];
} ATTRLIST, *PATTRLIST;

PATTRLIST p = malloc(sizeof(ATTRLIST) + sizeof(ATTR) * 10);

Tobias


Nice to know it is actually legal and portable! I've been using this a lot
and never took the time to check if it was an OK construct or if it gave me
the correct size for malloc.

Where in the standard is this defined? I've decided to actually read the
standard but am still in the beginnings of the document...


For features defined in C99 but not in C90, "legal" does not
necessarily imply "portable". The C99 standard is not univerally
implemented.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #7

In article <43******@griseus.its.uu.se>, Jan Danielsson <ja************@gmail.com> writes:

I'm writing an XML parser, and I'd like to know how portable a
solution is.

When I pass an STag or EmptyElement tag to the application, I
(obviously) need to pass the attribute list somehow. I thought about
storing the list in something like this:

typedef struct _ATTRLIST


Others have already discussed the "struct hack", but note also that
identifiers beginning with an underscore followed by an uppercase
letter are always reserved to the implementation. You cannot
portably use the struct tag "_ATTRLIST" for your own structure.
Similarly, any identifier that begins with an underscore is reserved
at file scope. Best bet: don't begin your identifiers with under-
scores. C90 7.1.3.

(Personally, I don't see why you use both a struct tag and a typedef,
since the struct doesn't contain a pointer to the same struct type.
My preference is to avoid typedef in nearly all situations, and
particularly to not disguise a pointer type using typedef, but those
are questions of style. Also, I avoid identifiers in block capitals;
some like to reserve those for macro names, but I use mixed case for
all identifiers as I've found that reduces the likelihood of name
collisions in the environments where my code is typically compiled.
That too is a style issue, of course.)

--
Michael Wojcik mi************@microfocus.com

It does basically make you look fat and naked - but you see all this stuff.
-- Susan Hallowell, TSA Security Lab Director, on "backscatter" scanners
Nov 15 '05 #8

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

Similar topics

1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
6
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
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; };
5
by: Cybertof | last post by:
Hello, Is it possible to convert a VB6 Array of Struct to a C# Array Of Struct ? The test context is a C# application calling a VB6 ActiveX DLL Function using UDT (User Defined Type) and...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
20
by: Cyn | last post by:
Hi, I want to create a general array structure which can hold all types. Something like this: struct ARRAY { void **array; size_t size; };
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
6
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.