This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: packed, aligned
- To: Dennis Bjorklund <db at zigo dot dhs dot org>
- Subject: Re: packed, aligned
- From: Bo Thorsen <bo at sonofthor dot dk>
- Date: Sun, 11 Feb 2001 11:32:00 +0100 (CET)
- Cc: <gcc at gcc dot gnu dot org>
On Sun, 11 Feb 2001, Dennis Bjorklund wrote:
> I'm very interested to know why one have choosen to not force the packed
> struct to be of minimal size.
For a struct like this:
struct bad_struct
{
char c1;
char *cp1;
char c2;
char *cp2;
};
on an architecture where pointers must be 4-byte aligned (I think sparc is
one such architecture) can not be packed. You could organize these
yourself like { *cp1, *cp2, c1, c2 } but doing that optimally by the
compiler is an NP-complete problem. Not to mention that some non-portable
applications could be depending on knowing the layout of the struct. If
someone would access these by offsets to the base pointer of the struct,
this would break. And even though I would probably consider this stupid,
it's perfectly legal and just because I can't see any use of it does not
mean there can't be a good reason for doing so.
Bottom line: This is a problem that no compiler will ever be able to
solve. It is a perfect example of the fact that compilers can never be so
good that they will just do what you want, and not what you told them to.
You have to be responsible for your own code and arrange the structs so
they can be packed. Yes, that sucks, but there's nothing you can do.
Bo.
--
Bo Thorsen | Lahnsgade 31, st.
Free software developer | 5000 Odense C
SuSE Labs | Denmark