🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3308

Re: Structure walking

#3308
From: Larry Jones
Date: Tue, 26 Jan 1993 15:44
75 lines
3968 bytes
In article <1993Jan25.213543.25499@pony.Ingres.COM>, mikes@Ingres.COM (Mike Schilling) writes:
> Question 1:  Are the following assumptions valid for all C compilers?
> For (c)-(f), assume that the structure contains only scalar members.
> 
> (a) Each scalar type has an unambiguous alignment requirement.

True, but the compiler may choose to use a stricter alignment than is
actually required.

> (b) Each scalar type's alignment requirement is a power of two.

False.  The Standard contains no such guarantee.

> (c) The offset of the first member of a structure is 0.

True.  ANSI 3.5.2.1 Structure and Union Specifiers:
	A pointer to a structure object, suitably converted, points to
	its initial member (or if that member is a bit-field, then to
	the unit in which it resides), and vice versa.  There may
	therefore be unnamed padding within a structure object, but not
	at its beginning, as necessary to achieve the appropriate
	alignment.

> (d) The offset of the (n+1)th member of a structure is the offset of
>     the nth member of the structure, plus the size of the nth member,
>     rounded up to the alignment requirement of the type of the (n+1)th member.

False.  ANSI 3.5.2.1 (emphasis mine):
	Each non-bit-field member of a structure or union object is
	aligned in an implementation-defined manner *appropriate* to its
	type.
Note that it says "appropriate to", not "required by".  An
implementation has a great deal of latitude in how it aligns members.

> (e) The alignment required by the structure is equal to the most restrictive
>     alignment of any of its members.

True, but see (a).

> (f) The size of the structure is the offset of the last member of the structure,
>     plus the size of the last member, rounded up to alignment required
>     by the structure.

False.  ANSI 3.5.2.1 again:
	There may also be unnamed padding at the end of a structure or
	union, as necessary to achieve the *appropriate* alignment were
	the structure or union to be an element of an array.
Again, it says "appropriate", not "required".

> Question 2:  Can the assumptions in question 1 be generalized to include
> structures which contain nested structures?  Equivalently: if we "flatten"
> a structure (eliminate all nesting), will that leave its size, alignment
> requirements, and the offsets of its scalar members unchanged?

The answers don't change if you allow nested structures but, since your
assumptions don't hold, you can't flatten a structure and expect its
size, alignment, and member offsets to remain the same.

> Question 3:  On at least one platform (Data General MV series minis),
> pointers to characters were represented diferently from pointers to anything
> else (really a byte pointer vs. word pointer distinction), so you had 
> to be very careful about how you cast pointers. 
> On such platforms, how are pointers to structures represented?  Does 
> it depend on the type of the structure?

It is generally reguarded as true that the Standard contains sufficient
requirements scattered about its various sections to require that all
structure pointers have the same representation.  There is no guarantee
that this representation is identical to the representation of any other
pointer type, however.
----
Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070
larry.jones@sdrc.com
If I get a bad grade, it'll be YOUR fault for not doing
the work for me! -- Calvin
Technical Details
Message-ID: <565@heimdall.sdrc.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!Germany.EU.net!news.netmbx.de!mailgzrz.TU-Berlin.DE!math.fu-berlin.de!ira.uka.de!sol.ctr.columbia.edu!emory!gatech!hubcap!ncrcae!ncrhub2!ncrgw2!psinntp!heimdall!thor!scjones
References: <1993Jan25.213543.25499@pony.Ingres.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!Germany.EU.net!news.netmbx.de!mailgzrz.TU-Berlin.DE!math.fu-berlin.de!ira.uka.de!sol.ctr.columbia.edu!emory!gatech!hubcap!ncrcae!ncrhub2!ncrgw2!psinntp!heimdall!thor!scjones
From: scjones@thor.sdrc.com (Larry Jones)
Newsgroups: comp.std.c
Subject: Re: Structure walking
Message-ID: <565@heimdall.sdrc.com>
Date: 26 Jan 93 15:44:26 GMT
References: <1993Jan25.213543.25499@pony.Ingres.COM>
Sender: news@heimdall.sdrc.com
Lines: 75