public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: packed, aligned
@ 2001-02-13 17:39 Mike Stump
  0 siblings, 0 replies; 9+ messages in thread
From: Mike Stump @ 2001-02-13 17:39 UTC (permalink / raw)
  To: db, graham; +Cc: gcc

> To: Dennis Bjorklund <db@zigo.dhs.org>
> Cc: <gcc@gcc.gnu.org>
> From: Graham Murray <graham@gmurray.org.uk>
> Date: 11 Feb 2001 11:20:26 +0000

> Dennis Bjorklund <db@zigo.dhs.org> writes:

> > Why would someone want to pack a struct and still have some padding in the
> > end?

> So that in an array of structures, each structure starts on an
> appropriate boundary. 

And byte aligned structures need only byte aligned boundaries, which
means zero padding.  All structures can be changed to be byte aligned
by the addition of attributes.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: packed, aligned
@ 2001-02-13 14:29 Richard Kenner
  0 siblings, 0 replies; 9+ messages in thread
From: Richard Kenner @ 2001-02-13 14:29 UTC (permalink / raw)
  To: fjh; +Cc: gcc

    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.

There's been talk recently of having GNAT do it.  A major reason it hasn't
been done in the past is the desire for compatibility with C structures.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: packed, aligned
  2001-02-11  1:44 Dennis Bjorklund
  2001-02-11  2:39 ` Bo Thorsen
  2001-02-11  3:21 ` Graham Murray
@ 2001-02-11 11:15 ` Richard Henderson
  2 siblings, 0 replies; 9+ messages in thread
From: Richard Henderson @ 2001-02-11 11:15 UTC (permalink / raw)
  To: Dennis Bjorklund; +Cc: gcc

On Sun, Feb 11, 2001 at 10:44:05AM +0100, Dennis Bjorklund wrote:
> Some time ago there was a discussion about packed structs and the fact
> that even if you pack a struct, the size of the struct according to
> sizeof() might not be the sum of the size of all the components.
> 
> Have I summed it up correctly so far?

No.  You can certainly pack a structure such that it has 
no padding whatsoever.  For example, 

	struct __attribute__((packed)) bad_struct
	{
	  char c1;
	  char *cp1;
	  char c2;
	  char *cp2;
	};

If this does not yield a structure of size (sizeof(char*)*2 + 2)
on any architecture, then it is a bug.  It certainly does on my
alpha.


r~

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: packed, aligned
@ 2001-02-11  4:57 dewar
  0 siblings, 0 replies; 9+ messages in thread
From: dewar @ 2001-02-11  4:57 UTC (permalink / raw)
  To: bo, db; +Cc: gcc

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

THat makes no sense to me. THere is no architecture in which it is not
possible to store a pointer misaligned, yes, of course reading it means
reading it in pieces and reassembling with shifts etc on some 
architectures, but so what, gcc can certainly do that. 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: packed, aligned
@ 2001-02-11  4:55 dewar
  0 siblings, 0 replies; 9+ messages in thread
From: dewar @ 2001-02-11  4:55 UTC (permalink / raw)
  To: db, gcc

<<I'm very interested to know why one have choosen to not force the packed
struct to be of minimal size.
>>

Well of course the size must be a multiple of the alignment, that's
fundamental. Not clear if that is the issue in the case you are talking
about.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: packed, aligned
  2001-02-11  2:39 ` Bo Thorsen
@ 2001-02-11  3:22   ` Fergus Henderson
  0 siblings, 0 replies; 9+ messages in thread
From: Fergus Henderson @ 2001-02-11  3:22 UTC (permalink / raw)
  To: Bo Thorsen; +Cc: Dennis Bjorklund, gcc

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: packed, aligned
  2001-02-11  1:44 Dennis Bjorklund
  2001-02-11  2:39 ` Bo Thorsen
@ 2001-02-11  3:21 ` Graham Murray
  2001-02-11 11:15 ` Richard Henderson
  2 siblings, 0 replies; 9+ messages in thread
From: Graham Murray @ 2001-02-11  3:21 UTC (permalink / raw)
  To: Dennis Bjorklund; +Cc: gcc

Dennis Bjorklund <db@zigo.dhs.org> writes:

> Why would someone want to pack a struct and still have some padding in the
> end?

So that in an array of structures, each structure starts on an
appropriate boundary. 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: packed, aligned
  2001-02-11  1:44 Dennis Bjorklund
@ 2001-02-11  2:39 ` Bo Thorsen
  2001-02-11  3:22   ` Fergus Henderson
  2001-02-11  3:21 ` Graham Murray
  2001-02-11 11:15 ` Richard Henderson
  2 siblings, 1 reply; 9+ messages in thread
From: Bo Thorsen @ 2001-02-11  2:39 UTC (permalink / raw)
  To: Dennis Bjorklund; +Cc: gcc

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* packed, aligned
@ 2001-02-11  1:44 Dennis Bjorklund
  2001-02-11  2:39 ` Bo Thorsen
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Dennis Bjorklund @ 2001-02-11  1:44 UTC (permalink / raw)
  To: gcc

Some time ago there was a discussion about packed structs and the fact
that even if you pack a struct, the size of the struct according to
sizeof() might not be the sum of the size of all the components.

Have I summed it up correctly so far?

But to pack a struct means that the padding between the components in the
struct is zero. Does this mean that the only extra padding is always in
the end of the struct, and there is no way to get rid of this padding?

Why would someone want to pack a struct and still have some padding in the
end?

In my opinion there really should be a packing that really packs a struct
and that is portable over all architectures that gcc runs on.

Either you use padding to exactly know where the different parts is or you
use it to save space and don't care about speed. In both cases it makes no
sence to have padding in the end. If you really want padding in the end
you could easily add that yourself, but now there is no way to remove
padding that you don't want.

I'm very interested to know why one have choosen to not force the packed
struct to be of minimal size.

-- 
/Dennis

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2001-02-13 17:39 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-13 17:39 packed, aligned Mike Stump
  -- strict thread matches above, loose matches on Subject: below --
2001-02-13 14:29 Richard Kenner
2001-02-11  4:57 dewar
2001-02-11  4:55 dewar
2001-02-11  1:44 Dennis Bjorklund
2001-02-11  2:39 ` Bo Thorsen
2001-02-11  3:22   ` Fergus Henderson
2001-02-11  3:21 ` Graham Murray
2001-02-11 11:15 ` Richard Henderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).