Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Flexible array member + variable length array

Reply
Thread Tools

Flexible array member + variable length array

 
 
Adam Warner
Guest
Posts: n/a
 
      02-03-2005
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[];
};

How does one initialise this structure using a variable length array?

ptrdiff_t len=10;
{
struct array a[len+1];
...
}

It appears the above code will only work to reserve space for len pointers
if the sizeof length is the same as sizeof pointers to void and there is
no padding between length and the pointers to void. Is there portable C99
syntax that I have overlooked? (or will I have to create a VLA of type
void * and keep casting the first argument to ptrdiff_t?)

Thanks,
Adam
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      02-03-2005
Adam Warner wrote:
> 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[];
> };
>
> How does one initialise this structure using a variable length array?
>
> ptrdiff_t len=10;
> {
> struct array a[len+1];
> ...
> }
>
> It appears the above code will only work to reserve space for len pointers
> if the sizeof length is the same as sizeof pointers to void and there is
> no padding between length and the pointers to void. Is there portable C99
> syntax that I have overlooked? (or will I have to create a VLA of type
> void * and keep casting the first argument to ptrdiff_t?)


I am not sure what you are asking for. The example from the standard
runs like this:

(6.7.2.1)
"
17
EXAMPLE Assuming that all array members are aligned the same, after the
declarations:
struct s { int n; double d[]; };
struct ss { int n; double d[1]; };
the three expressions:
sizeof (struct s)
offsetof(struct s, d)
offsetof(struct ss, d)
have the same value. The structure struct s has a flexible array member
d.

18
If sizeof (double) is 8, then after the following code is executed:
struct s *s1;
struct s *s2;
s1 = malloc(sizeof (struct s) + 64);
s2 = malloc(sizeof (struct s) + 46);
and assuming that the calls to malloc succeed, the objects pointed to by
s1 and s2 behave as if the identifiers had been declared as:
struct { int n; double d[8]; } *s1;
struct { int n; double d[5]; } *s2;
"

Structures with flexible array members of course cannot be
put into arrays.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Shan
Guest
Posts: n/a
 
      02-03-2005
Visit this page:

http://gcc.gnu.org/ml/gcc/2002-04/msg00026.html

Hope it helps.

Cheers
Shan

Adam Warner wrote:
> 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[];
> };
>
> How does one initialise this structure using a variable length array?
>
> ptrdiff_t len=10;
> {
> struct array a[len+1];
> ...
> }
>
> It appears the above code will only work to reserve space for len

pointers
> if the sizeof length is the same as sizeof pointers to void and there

is
> no padding between length and the pointers to void. Is there portable

C99
> syntax that I have overlooked? (or will I have to create a VLA of

type
> void * and keep casting the first argument to ptrdiff_t?)
>
> Thanks,
> Adam


 
Reply With Quote
 
Shan
Guest
Posts: n/a
 
      02-03-2005

Shan wrote:
> Visit this page:
>
> http://gcc.gnu.org/ml/gcc/2002-04/msg00026.html
>
> Hope it helps.
>
> Cheers
> Shan
>
> Adam Warner wrote:
> > 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[];
> > };
> >
> > How does one initialise this structure using a variable length

array?
> >
> > ptrdiff_t len=10;
> > {
> > struct array a[len+1];
> > ...
> > }
> >
> > It appears the above code will only work to reserve space for len

> pointers
> > if the sizeof length is the same as sizeof pointers to void and

there
> is
> > no padding between length and the pointers to void. Is there

portable
> C99
> > syntax that I have overlooked? (or will I have to create a VLA of

> type
> > void * and keep casting the first argument to ptrdiff_t?)
> >
> > Thanks,
> > Adam


Sorry, i top posted by mistake....

 
Reply With Quote
 
Adam Warner
Guest
Posts: n/a
 
      02-03-2005
On Thu, 03 Feb 2005 10:29:48 +0100, Michael Mair wrote:

> EXAMPLE Assuming that all array members are aligned the same, after the
> declarations:
> struct s { int n; double d[]; };
> struct ss { int n; double d[1]; };
> the three expressions:
> sizeof (struct s)
> offsetof(struct s, d)
> offsetof(struct ss, d)
> have the same value. The structure struct s has a flexible array member
> d.
>
> 18
> If sizeof (double) is 8, then after the following code is executed:
> struct s *s1;
> struct s *s2;
> s1 = malloc(sizeof (struct s) + 64);
> s2 = malloc(sizeof (struct s) + 46);
> and assuming that the calls to malloc succeed, the objects pointed to by
> s1 and s2 behave as if the identifiers had been declared as:
> struct { int n; double d[8]; } *s1;
> struct { int n; double d[5]; } *s2;
> "
>
> Structures with flexible array members of course cannot be
> put into arrays.


Indeed. It looks like I was after alloca-style functionality (which is
non-standard) to duplicate the example above on the stack instead of the
heap.

Regards,
Adam
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      02-03-2005
On Thu, 03 Feb 2005 20:50:36 +1300, Adam Warner wrote:

> 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[];
> };
>


This is a structure containing a C99 flexible array member.

> How does one initialise this structure using a variable length array?


You don't. Flexible array members and VLAs are different and unrelated
constructs. Flexible array members are designed to be allocated using
malloc() and friends, e.g.

struct array *p = malloc(sizeof *p + len * sizeof *p->ptr);

> ptrdiff_t len=10;
> {
> struct array a[len+1];
> ...
> }


You can't make arrays of structures containing flexible array members
because the size of each element is not known or fixed.

Lawrence
 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      02-04-2005
Lawrence Kirby <> wrote:
> On Thu, 03 Feb 2005 20:50:36 +1300, Adam Warner wrote:


> > 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[];
> > };
> >


> This is a structure containing a C99 flexible array member.


> > How does one initialise this structure using a variable length array?


> You don't. Flexible array members and VLAs are different and unrelated
> constructs. Flexible array members are designed to be allocated using
> malloc() and friends, e.g.


> struct array *p = malloc(sizeof *p + len * sizeof *p->ptr);


Well, if an object was declared with the type struct with FAM, then
you actually could initialize it's leading members, couldn't you?
(Of course, declaring such objects would be against the purpose
of FAM.)
(Note: you cannot initialize FAM itself, even if the struct were
the first member of a suitable union, because FAM is ignored in
all contexts, except `sizeof', `.' and `->'.)

> > ptrdiff_t len=10;
> > {
> > struct array a[len+1];
> > ...
> > }


> You can't make arrays of structures containing flexible array members
> because the size of each element is not known or fixed.


Would you please kindly elaborate on that?

Indeed, FAMs (flexible array members) and VLAs are different. FAMs are
described in 6.7.2.1p16. The size of a struct with a FAM is the
offset of the FAM, therefore such a struct is not an incomplete type.
It is not variably modified type either, because it does not contain a
VLA type (the definition of VM types is in 6.7.5p3).

The last place I looked was in array declarators, and all I found was
in 6.7.5.2p1:
# The element type shall not be an incomplete or function type.

I see nothing that would forbid to create arrays of structs with FAM.

+++

However, I tried this program with Comeau compiler in C99 mode:

struct flex
{
int i;
int tail[];
};

int main()
{
struct flex f;
int s = sizeof(struct flex);
struct flex a[5];
}

[...]
"t.c", line 11: error: type containing an unknown-size array is not allowed
struct flex a[5];
^

It seems to accept a declared object (`f') of type struct flex, but
rejects an array for some reason.

--
Stan Tobias
mailx `echo LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      02-05-2005
S.Tobias wrote:
> Lawrence Kirby <> wrote:
>
>>On Thu, 03 Feb 2005 20:50:36 +1300, Adam Warner wrote:

>
>
>>>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[];
>>>};
>>>

>
>
>>This is a structure containing a C99 flexible array member.

>
>
>>>How does one initialise this structure using a variable length array?

>
>
>>You don't. Flexible array members and VLAs are different and unrelated
>>constructs. Flexible array members are designed to be allocated using
>>malloc() and friends, e.g.

>
>
>> struct array *p = malloc(sizeof *p + len * sizeof *p->ptr);

>
>
> Well, if an object was declared with the type struct with FAM, then
> you actually could initialize it's leading members, couldn't you?
> (Of course, declaring such objects would be against the purpose
> of FAM.)
> (Note: you cannot initialize FAM itself, even if the struct were
> the first member of a suitable union, because FAM is ignored in
> all contexts, except `sizeof', `.' and `->'.)
>
>
>>>ptrdiff_t len=10;
>>>{
>>> struct array a[len+1];
>>> ...
>>>}

>
>
>>You can't make arrays of structures containing flexible array members
>>because the size of each element is not known or fixed.

>
>
> Would you please kindly elaborate on that?
>
> Indeed, FAMs (flexible array members) and VLAs are different. FAMs are
> described in 6.7.2.1p16. The size of a struct with a FAM is the
> offset of the FAM, therefore such a struct is not an incomplete type.
> It is not variably modified type either, because it does not contain a
> VLA type (the definition of VM types is in 6.7.5p3).
>
> The last place I looked was in array declarators, and all I found was
> in 6.7.5.2p1:
> # The element type shall not be an incomplete or function type.
>
> I see nothing that would forbid to create arrays of structs with FAM.
>
> +++
>
> However, I tried this program with Comeau compiler in C99 mode:
>
> struct flex
> {
> int i;
> int tail[];
> };
>
> int main()
> {
> struct flex f;
> int s = sizeof(struct flex);
> struct flex a[5];
> }
>
> [...]
> "t.c", line 11: error: type containing an unknown-size array is not allowed
> struct flex a[5];
> ^
>
> It seems to accept a declared object (`f') of type struct flex, but
> rejects an array for some reason.


Because there is no guarantee that the alignment requirements hold
in the sense that there is padding at the end.
Example: Let sizeof(int)==4, sizeof(short)==2 and the alignment
requirements equal to the size of the type.
Hence,
struct s1 {
int i;
char c;
}
will have a size of 8, but
struct s2 {
int i;
char c;
short s[];
}
has as size of 6 as we assume that
sizeof(structs2)==offsetof(struct s2,s)
and offsetof(struct s2,s)==offsetof(struct s3,s)
with
struct s3 {
int i;
char c;
short s[1];
}
which follows from the (non-normative) example 6.7.2.1#17


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      02-06-2005
Michael Mair <> wrote:
> S.Tobias wrote:
> > Lawrence Kirby <> wrote:


[snippage]

> >>You can't make arrays of structures containing flexible array members
> >>because the size of each element is not known or fixed.

> >
> >
> > Would you please kindly elaborate on that?
> >
> > Indeed, FAMs (flexible array members) and VLAs are different. FAMs are
> > described in 6.7.2.1p16. The size of a struct with a FAM is the
> > offset of the FAM, therefore such a struct is not an incomplete type.
> > It is not variably modified type either, because it does not contain a
> > VLA type (the definition of VM types is in 6.7.5p3).
> >
> > The last place I looked was in array declarators, and all I found was
> > in 6.7.5.2p1:
> > # The element type shall not be an incomplete or function type.
> >
> > I see nothing that would forbid to create arrays of structs with FAM.
> >
> > +++
> >
> > However, I tried this program with Comeau compiler in C99 mode:
> >

[snip code]
> >
> > [...]
> > "t.c", line 11: error: type containing an unknown-size array is not allowed
> > struct flex a[5];
> > ^
> >
> > It seems to accept a declared object (`f') of type struct flex, but
> > rejects an array for some reason.


Thank you for answering, however I disagree with your POV.

> Because there is no guarantee that the alignment requirements hold
> in the sense that there is padding at the end.


But the Standard does not make any requirements for alignment
(it merely allows them); it's up to the implementation fulfill them.

> Example: Let sizeof(int)==4, sizeof(short)==2 and the alignment
> requirements equal to the size of the type.
> Hence,
> struct s1 {
> int i;
> char c;
> }
> will have a size of 8, but
> struct s2 {
> int i;
> char c;
> short s[];
> }
> has as size of 6 as we assume that
> sizeof(structs2)==offsetof(struct s2,s)


Yes, but what's the point? On an architecture without alignment
this shouldn't be a problem, should it?

I think you use your logic in the wrong direction. My reasoning
would be that since the Standard doesn't forbid structs with FAM
being elements of an array, they are allowed. It means that on
an implementation with int alignment 4 bytes, the compiler must
provide padding bits so that sizeof(struct s2) == 8.

> and offsetof(struct s2,s)==offsetof(struct s3,s)
> with
> struct s3 {
> int i;
> char c;
> short s[1];
> }
> which follows from the (non-normative) example 6.7.2.1#17


No, the Standard is very clear that it does not guarantee this
(footnote 106; and the word "Assuming" in the example).

Note that 6.7.2.1p16 does not provide any recipe for building
struct s2 type; it uses words "an array of unspecified length",
which means it is up to the implementation to decide what length
should be the most suitable.

+++

I think it not wrong to imagine that a FAM is a "member" which
represents the bytes _after_ the body of the struct object
(ie. the FAM does not strictly belong to the struct).

===

For others' convenience, here's the full quote of 6.7.2.1p16:

# 16 As a special case, the last element of a structure with more
# than one named member may have an incomplete array type;
# this is called a flexible array member. With two exceptions,
# the flexible array member is ignored. First, the size of the
# structure shall be equal to the offset of the last element of
# an otherwise identical structure that replaces the flexible
# array member with an array of unspecified length.106) Second,
# when a . (or ->) operator has a left operand that is (a pointer
# to) a structure with a flexible array member and the right
# operand names that member, it behaves as if that member were
# replaced with the longest array (with the same element type)
# that would not make the structure larger than the object being
# accessed; the offset of the array shall remain that of the
# flexible array member, even if this would differ from that of
# the replacement array. If this array would have no elements, it
# behaves as if it had one element but the behavior is undefined
# if any attempt is made to access that element or to generate
# a pointer one past it.
#
# 106) The length is unspecified to allow for the fact that
# implementations may give array members different alignments
# according to their lengths.

--
Stan Tobias
mailx `echo LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
Kevin Bracey
Guest
Posts: n/a
 
      02-10-2005
In message <>
"S.Tobias" <> wrote:

> I think you use your logic in the wrong direction. My reasoning
> would be that since the Standard doesn't forbid structs with FAM
> being elements of an array, they are allowed. It means that on
> an implementation with int alignment 4 bytes, the compiler must
> provide padding bits so that sizeof(struct s2) == 8.


Wrong. 6.7.2.1p2:

"...the last member of a structure with more than one named member
may have incomplete array type; such a structure ... shall not be a
member of a structure or an element of an array."

This is precisely to cope with alignment issues. On our implementation,
int has alignment 4, and given

struct s1 {
int i;
char c;
};

struct s2 {
int i;
char c;
short s[];
};

struct s1 has size 8, and struct s2 has size 6 (because sizeof(struct s2)
must equal offsetof(struct s2, s)), just as Michael suggests. Thus any
struct s2s placed in an array would be misaligned. Fortunately, the standard
forbids this.

There is a Defect Report #282 about sizeof such a structure, but it's bogus
as far as I can see - they're suggesting that they have to put padding before
s[] to make s2 be size 8 for alignment, but what they've missed is that s2
does not have to meet alignment requirements, because of the array
restriction, so s2 can be left unpadded.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Alignment of struct with flexible array member Hallvard B Furuseth C Programming 3 12-13-2006 11:24 PM
Flexible array member and alignement tobiasoed@hotmail.com C Programming 2 05-29-2006 12:51 PM
flexible array member DevarajA C Programming 2 11-13-2005 07:53 AM
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
Passing list with flexible length to C extension =?utf-8?Q?Bo=C5=A1tjan?= Jerko Python 1 11-21-2003 01:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57