Article View: comp.std.c
Article #3304Re: Structure walking
From: Norman Diamond
Date: Tue, 26 Jan 1993 01:36
Date: Tue, 26 Jan 1993 01:36
74 lines
4041 bytes
4041 bytes
In article <1993Jan25.213543.25499@pony.Ingres.COM> mikes@Ingres.COM (Mike Schilling) writes: >Instead, we propose to store a template indicating the type of each member >in the structure being encoded, followed by a portable representation of >each scalar; And just write/read an array of chars, right? Or use the XDR representations defined by one vendor (not standard but easy to use for your purposes). >The tricky part is inferring the offset of each member within the structure. Why is it insufficient to just use "offsetof" on each member? >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. For all existing C compilers? Probably. For all hypothetical standard conforming compilers? Uncertain. Actually it is imaginable, for a hypothetical but really meaningful compiler, if space is virtually free but speed is extremely expensive, one might want to align a 4-byte integer in the second half of an 8-byte cache line in order to save time by only filling half of a cache line. I don't know if you'd call this unambiguous. >(b) Each scalar type's alignment requirement is a power of two. For all existing C compilers, probably. For all hypothetical standard conforming compilers, no, very clearly no. >(c) The offset of the first member of a structure is 0. Probably. A structure pointer cast to the type of the structure's first member, or vice-versa, must still point to the right place. And there cannot be a "hole" at the beginning of a structure. I'm not sure if this proves that "offsetof" that member must yield a 0 result. >(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. I think the answer to (a) fits here. >(e) The alignment required by the structure is equal to the most restrictive > alignment of any of its members. No. It seems to be mostly agreed (though I'm not sure if I agree) that the standard permits, and in some cases compilers do, align structures more strictly than the most restrictive alignment of any of its members. (This also answers part of your Question 3.) >(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. I think the answer to (a) fits again here. Incidentally, it was not necessary to assume, for (c)-(f), that the structure contains only scalar members. (This takes care of your Question 2.) >Question 3: On at least one platform, pointers to characters were represented >diferently from pointers to anything else >On such platforms, how are pointers to structures represented? I think it's unspecified. The implementation has to do something that will work, but I don't think they have to tell you what they've done. >Does it depend on the type of the structure? No. The standard doesn't say, but someone has proven from other things that the standard says, that all structure pointers must be represented identically. -- Norman Diamond diamond@jit.dec.com If this were the company's opinion, I wouldn't be allowed to post it. Pardon me? Or do I have to commit a crime first?
Technical Details
Message-ID:
<1993Jan26.013634.20451@nntpd.lkg.dec.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!olivea!decwrl!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamondReferences:
<1993Jan25.213543.25499@pony.Ingres.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!olivea!decwrl!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond From: diamond@jit345.bad.jit.dec.com (Norman Diamond) Newsgroups: comp.std.c Subject: Re: Structure walking Message-ID: <1993Jan26.013634.20451@nntpd.lkg.dec.com> Date: 26 Jan 93 01:36:34 GMT References: <1993Jan25.213543.25499@pony.Ingres.COM> Sender: usenet@nntpd.lkg.dec.com (USENET News System) Reply-To: diamond@jit.dec.com (Norman Diamond) Organization: Digital Equipment Corporation Japan , Tokyo Lines: 74