Variable Sized Struct Members?

M

Michael B Allen

My understanding is that struct members will remain in order such that
the following can be used to support variable sized members:

struct indexed_values {
int *values;
unsigned char bitset[0];
};

struct meta_data {
struct indexed_values foo;
unsigned char _foo_bs[8];
struct indexed_values bar;
unsigned char _bar_bs[16];
struct indexed_values zap;
unsigned char _zap_bs[4];
....
};

In this case accessing meta_data->bar[15] for example cannot overlap
with other members of runover into the zap member.

Is this legitimate C?

Thanks,
Mike
 
D

Derrick Coetzee

Michael said:
My understanding is that struct members will remain in order such that
the following can be used to support variable sized members:

struct indexed_values {
int *values;
unsigned char bitset[0];
};

You can't have length-zero arrays - this is only a popular but
noncompliant extension that is really only useful in this case. See the
ANSI C Rationale section 3.5.4.2:

http://www.lysator.liu.se/c/rat/c5.html#3-5-4-2

More disappointingly, even the method described by the Rationale is
technically noncompliant - see question 2.6 of the FAQ:

http://www.eskimo.com/~scs/C-faq/q2.6.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top