This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: packed, aligned


On 11-Feb-2001, Bo Thorsen <bo@sonofthor.dk> wrote:
> 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.

So is optimal register allocation.  But nevertheless compilers manage
to do register allocation nonetheless!  And although they don't do an
*optimal* job of it, they generally do a reasonably good job, at any
rate good enough to make it not worthwhile for programmers to do
register allocation by hand.

The problem is not that it would be hard for a compiler to do.
The main problem is just that the C standard forbids it.

> Bottom line: This is a problem that no compiler will ever be able to
> solve.

No C compiler, true.  But C is not the only language in the world.

> 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.

Except use a different language.

For example, if you write equivalent code in Ada, i.e.

	type char_ptr is access all character;

	type bad_struct is record
	  c1 : aliased character;
	  cp1 : char_ptr;
	  c2 : aliased character;
	  cp2 : char_ptr;
	end record;

	pragma pack (bad_struct);

then the Ada standard specifically gives implementations permission to
reorder the fields so that cp1 and cp2 can share the same word.

However, the Ada standard doesn't require implementations to do so,
and as it turns out GNU Ada (version 3.13p) doesn't do it.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]