Article View: comp.std.c
Article #75Re: Character arrays in structures and address alignment -- any standard?
From: T. William Wells
Date: Fri, 31 Jul 1992 21:42
Date: Fri, 31 Jul 1992 21:42
71 lines
3140 bytes
3140 bytes
In article <1992Jul30.175841.7597@taumet.com> steve@taumet.com (Steve Clamage) writes:
: 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).
Right.
However, I wanted to illustrate the general case. I think that it
would have to be zero even in the general case.
: 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.
Not necessarily. E.g.: ints have alignment sizeof(int) but all
struct have lcm(sizeof(int)*4,sizeof(member1),...) alignment.
sizeof (int [2]) would be sizeof(int)*2 but sizeof(foo) would be
sizeof(int)*4. In the usual case, though, the alignment of the
structure would be determined only by its members and you'd be
right.
: 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.
Actually, if padding may only be added to maintain alignment,
padding would not be allowed between a and b in this case.
: "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.
Necessary is used to modify alignment in the C standard sense, not
the target machine sense. That is to say, the implementer decides
what alignments are necessary and makes the compiler adhere to it.
(Translation: I agree. :-)
---
Bill { uunet | decwrl | telesci }!twwells!bill
bill@twwells.com
Technical Details
Message-ID:
<Bs9wAw.1nw@twwells.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!twwells!billReferences:
<14216@ksr.com> <Bs5pMJ.3nM@twwells.com> <1992Jul30.175841.7597@taumet.com>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!sparky!uunet!twwells!bill From: bill@twwells.com (T. William Wells) Subject: Re: Character arrays in structures and address alignment -- any standard? Organization: None, Mt. Laurel, NJ References: <14216@ksr.com> <Bs5pMJ.3nM@twwells.com> <1992Jul30.175841.7597@taumet.com> Message-ID: <Bs9wAw.1nw@twwells.com> Date: Fri, 31 Jul 1992 21:42:30 GMT Lines: 71