On Jun 9, 2:27*am, Heikki Kallasjoki <fis+use...@zem.fi> wrote:
> Besides the obvious thing of being able to refer to it by name, there is
> at least the reason that the compiler will make sure that the "Entries"
> member is properly aligned for SOMETYPE. *That is not necessarily the
> case for malloc(...)+sizeof(ALTSTRUCT).
OK, alignment is a good reason. Referring by name may not even work
though, for instance
there are other structs like for EMR_POLYPOLYLINE (not sure how stable
this link will be)
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
where the struct ends in:
DWORD aPolyCounts[1];
POINTL aptl[1];
Hard to imagine the situation where aptl could ever be successfully
referenced by name when aPolyCounts is
also used.
>
> A C99 "flexible array member" (which does not count in sizeof of the
> structure type) would possibly be the optimal solution, were C99 support
> universal.
I thought that structs including flexible array members were not
allowed to be members of other structs (or used in arrays, not that
that applies here). Otherwise flexible array members would be good
here because sizeof() would ignore the variable array on the end.
Basically the problem is that since the run time data structure in
memory (or on disk) looks like:
<struct1><variable length field(s)><struct2><variable length
field(s)>
there really is no way at compile time to reliably reference anything
but the members of struct1 by name using a pointer to the beginning of
this data assembly. (And then only if there aren't 2 arrays on the
end of struct1!) The compiler can generate name references to members
of struct2, but only with reference to a new memory pointer to the
beginning of that struct, and that pointer must be constructed at run
time.
Tnanks,
David Mathog