Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Richard Damon <news.x.richarddamon@xoxy.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: partial struct usage |
| Date | 2011-09-24 13:44 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <j5l4tk$t2d$1@dont-email.me> (permalink) |
| References | <2011092410234736215-madler@alumnicaltechedu> |
On 9/24/11 1:23 PM, Mark Adler wrote:
> I have an internal structure with lots of stuff in it for controlling a
> data input operation. E.g.:
>
> struct data {
> int have;
> char *next;
> ... lots more stuff to keep track of the state
> };
>
> I would like to expose to the user (in the interface header file) only
> those first two elements, so that I can provide a macro (also in the
> header file) analogous to getc() to pull data that is readily available
> in the buffer, otherwise to call a function to go get more data. But I
> want to hide the other stuff in the structure, in order to make sure
> that users of the library don't end up depending on things that I change
> later for internal use. And for efficiency I don't want yet another
> layer of indirection pointing to another structure from within the
> exposed structure for the other stuff.
>
> E.g.
>
> struct ex {
> int have;
> char *next;
> };
>
> #define getd(d) (((struct ex *)d)->have ? (((struct ex *)d)->have--,
> *(((struct ex *)d)->next)++) : getdf(d))
>
> where d is a struct data *.
>
> My question is: does the C standard provide an assurance that this will
> work? I.e. that two different structures with identical sets of prefix
> elements will have those prefix elements layed out identically in memory
> so that casting one structure to the other will permit correct access to
> those prefix elements?
>
> Mark
>
If you look at the specifications for unions, you will see a requirement
for access compatibility for structs with equivalent initial sequences.
While it technically means it only applies in unions, to make it work
for unions but not elsewhere is hard, so unless you are using a "debug"
version of the compiler checking for this sort of "error", it should work.
Another method it to place the "public" members in a separate stuct
which it the first member of the full struct, then use the fact that the
address of the first member of a struct is the same as the address of
the struct (which means the cast gives a usable value). No pointer
indirection costs, (just a level of . syntax).
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
partial struct usage Mark Adler <madler@alumni.caltech.edu> - 2011-09-24 10:23 -0700
Re: partial struct usage Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-24 13:44 -0400
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 14:08 -0800
Re: partial struct usage BGB <cr88192@hotmail.com> - 2011-09-24 11:08 -0700
Re: partial struct usage Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-24 23:36 +0300
Re: partial struct usage Mark Adler <madler@alumni.caltech.edu> - 2011-09-25 13:23 -0700
Re: partial struct usage Gene <gene.ressler@gmail.com> - 2011-09-26 01:14 -0700
Re: partial struct usage Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-26 12:37 -0400
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:04 -0800
Re: partial struct usage Mark Adler <madler@alumni.caltech.edu> - 2011-09-26 18:52 -0700
Re: partial struct usage Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-27 12:14 +0100
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 15:56 -0800
Re: partial struct usage Shao Miller <sha0.miller@gmail.com> - 2012-01-24 23:30 -0500
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 16:11 -0800
Re: partial struct usage Shao Miller <sha0.miller@gmail.com> - 2012-02-01 00:42 -0500
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 09:33 -0800
Re: partial struct usage Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-12 02:10 +0200
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 09:38 -0800
Re: partial struct usage Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-03-13 18:22 +0200
Re: partial struct usage blp@cs.stanford.edu (Ben Pfaff) - 2012-03-13 09:55 -0700
Re: partial struct usage Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-24 17:56 -0400
csiph-web