🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #168

Re: Character arrays in structures and address alignment -- any standard?

#168
From: Steve Clamage
Date: Thu, 30 Jul 1992 17:58
53 lines
2346 bytes
bill@twwells.com (T. William Wells) writes:

>In article <14216@ksr.com> jfw@ksr.com (John F. Woods) writes:
>: Please show a strictly conforming program which can tell the difference
>: between "necessary" padding and "unnecessary" padding.

>For example:

>typedef struct {
>	int     x;
>	int     y;
>} foo;
>foo z;

>main()
>{
>	printf("%d\n", (offsetof(foo, y) - offset(foo, x)) - sizeof(int));
>	return (0);
>}

>should print 0 on any standards conforming compiler, if it is true
>that the standard permits only padding for the purpose of
>creating proper alignment.

A good case could be made for this particular example that 0 must be
printed.  (The offset of x must be zero, so the example can be simplified).

The sizeof operator returns the size of an object as used in an array,
with whatever padding is added.  One would then expect that array of
two ints and "foo" would be the same size.  There is the possibility
that padding in an array is not necessary but padding in a struct
is necessary.  It seems unlikely, but I don't know that the Standard
forbids such a situation.

The original example was something like
	typedef struct {
	    char a[3];
	    char b[3];
	}foo;
and the question was whether offsetof(foo,b) must be 3.  I don't believe
a strictly-conforming program could determine whether padding between
fields "a" and "b" is "necessary", but only whether it was present.

"Necessary" is kind of a vague term.  On some architectures, alignment
is absolutely required to avoid runtime faults.  Certainly this is
"necessary".  On other architectures, alignment results in code which
runs considerably faster at the expense of increased data space.  Is
alignment then "necessary"?  I believe that is an economic, not a
Standards, question.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com
Vice Chair, ANSI C++ Committee, X3J16
Technical Details
Message-ID: <1992Jul30.175841.7597@taumet.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!taumet!steve
References: <1992Jul27.183622.794@taumet.com> <1992Jul28.063356.28005@sq.sq.com> <14216@ksr.com> <Bs5pMJ.3nM@twwells.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!taumet!steve
From: steve@taumet.com (Steve Clamage)
Subject: Re: Character arrays in structures and address alignment -- any standard?
Message-ID: <1992Jul30.175841.7597@taumet.com>
Organization: TauMetric Corporation
References: <1992Jul27.183622.794@taumet.com> <1992Jul28.063356.28005@sq.sq.com> <14216@ksr.com> <Bs5pMJ.3nM@twwells.com>
Date: Thu, 30 Jul 1992 17:58:41 GMT
Lines: 53