E
Eric Smith
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 22 of
section 6.2.5. If so, that seems to make it difficult to allocate such
structures, because sizeof() is not allowed on incomplete types
(paragraph 1 of section 6.5.3.4).
For instance, I've routinely done things like this:
struct foo { int a; char b; double c []; };
struct foo *alloc_foo (int s)
{
return (calloc (1, sizeof (struct foo) + sizeof (double) * s));
}
But section 6.2.5 seems to make this invalid.
Is this the correct interpretation? If so, is there some other
valid way to implement alloc_foo()?
On the other hand, paragraph 3 of section 6.7.2.3 states that the
declaration of a structure type is complete after the closing brace,
without any caveat about incomplete elements of the structure.
In writing this, I've started to wonder whether it's really specified
that the size of an array of a particular element type is equal to the
product of the array size and the size of the element type. I can't
find anything suggesting that this is required. If it's not, is there
some way given an integer expression to get the size of an array of that
many elements of a particular type? Something like sizeof(double)?
I used to think that using Ada's constrained record types was a hassle,
but now I'm not convinced that the C99 incomplete array in a structure
feature is any better.
Thanks,
Eric Smith
[I'll watch for replies to the newsgroup, but if you want to reply by
email, please remove the obvious spam-proofing from my email address.]
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 22 of
section 6.2.5. If so, that seems to make it difficult to allocate such
structures, because sizeof() is not allowed on incomplete types
(paragraph 1 of section 6.5.3.4).
For instance, I've routinely done things like this:
struct foo { int a; char b; double c []; };
struct foo *alloc_foo (int s)
{
return (calloc (1, sizeof (struct foo) + sizeof (double) * s));
}
But section 6.2.5 seems to make this invalid.
Is this the correct interpretation? If so, is there some other
valid way to implement alloc_foo()?
On the other hand, paragraph 3 of section 6.7.2.3 states that the
declaration of a structure type is complete after the closing brace,
without any caveat about incomplete elements of the structure.
In writing this, I've started to wonder whether it's really specified
that the size of an array of a particular element type is equal to the
product of the array size and the size of the element type. I can't
find anything suggesting that this is required. If it's not, is there
some way given an integer expression to get the size of an array of that
many elements of a particular type? Something like sizeof(double
I used to think that using Ada's constrained record types was a hassle,
but now I'm not convinced that the C99 incomplete array in a structure
feature is any better.
Thanks,
Eric Smith
[I'll watch for replies to the newsgroup, but if you want to reply by
email, please remove the obvious spam-proofing from my email address.]