Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Generic read/write function - casting vs union |
| Date | 2016-09-22 11:07 +0100 |
| Organization | Fix this later |
| Message-ID | <ns0ah9$uhd$1@dont-email.me> (permalink) |
| References | <ns08ge$o5v$1@dont-email.me> |
On 22/09/16 10:33, Noob wrote:
> Hello everyone,
>
> I'm required to implement a read() function and a write() function.
> (The prototype of these functions is not under my control.)
>
> void read(void *dev, void *buf, int len);
> void write(void *dev, const void *buf, int len);
>
> There is a lot of code in common between the two implementations:
>
> read:
> COMMON PROLOG
> do the actual read
> COMMON EPILOG
>
> write:
> COMMON PROLOG
> do the actual write
> COMMON EPILOG
>
> Therefore, to avoid code duplication, I implemented a generic
> access() function:
>
> void access(void *dev, void *buf, int len, int dir);
> COMMON PROLOG
> if (dir == READ) do the actual read
> if (dir == WRITE) do the actual write
> COMMON EPILOG
>
> then I implemented read() and write() as wrappers around access()
>
> read = access(dev, buf, len, READ);
> write = access(dev, (void *)buf, len, WRITE);
>
> As you can see, I had to cast away the constness of the write buffer,
> because my generic function does change buf, only in the read case.
> But I think there is no undefined behavior, because I do not modify
> the const buf. Is that correct?
Yes. Beware, however, of the name access(), as it is a common extension.
(In fact, I'm pretty sure it's in POSIX.)
> Some people frowned upon this cast, and suggested a union.
>
> union buf { void *in; const void *out; };
> void access(void *dev, union buf *buf, int len, int dir)
> COMMON PROLOG
> if (dir == READ) do the actual read on buf->in
> if (dir == WRITE) do the actual write on buf->out
> COMMON EPILOG
>
> That does solve the issue "casting away const is bad", but it
> seems not very idiomatic (to me). Do people consider this a
> better solution than casting away const?
On this occasion, no. I'm not a fan of casting away const (or indeed
casts in general), but in this case it's the lesser of two evils.
>
>
> Something I didn't mention is that the access function actually
> updates buf in the EPILOG.
>
> buf->in += len;
>
> Is it *100%* guaranteed that incrementing buf->in is strictly
> equivalent to incrementing buf->out ?
You can't do pointer arithmetic on void *. :-)
But you can assign to an unsigned char *, and increment that instead.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-22 11:33 +0200
Re: Generic read/write function - casting vs union Richard Heathfield <rjh@cpax.org.uk> - 2016-09-22 11:07 +0100
Re: Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-22 12:36 +0200
Re: Generic read/write function - casting vs union Richard Heathfield <rjh@cpax.org.uk> - 2016-09-22 14:07 +0100
Re: Generic read/write function - casting vs union Keith Thompson <kst-u@mib.org> - 2016-09-22 11:48 -0700
Re: Generic read/write function - casting vs union raltbos@xs4all.nl (Richard Bos) - 2016-09-23 19:42 +0000
Re: Generic read/write function - casting vs union BartC <bc@freeuk.com> - 2016-09-22 13:11 +0100
Re: Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-22 14:46 +0200
Re: Generic read/write function - casting vs union David Brown <david.brown@hesbynett.no> - 2016-09-22 15:10 +0200
Re: Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-22 15:36 +0200
Re: Generic read/write function - casting vs union Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-22 19:28 -0700
Re: Generic read/write function - casting vs union David Brown <david.brown@hesbynett.no> - 2016-09-23 09:59 +0200
Re: Generic read/write function - casting vs union Barry Schwarz <schwarzb@dqel.com> - 2016-09-22 20:11 -0700
Re: Generic read/write function - casting vs union Richard Damon <Richard@Damon-Family.org> - 2016-09-23 11:13 -0400
Re: Generic read/write function - casting vs union Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-22 08:35 -0700
Re: Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-25 23:17 +0200
Re: Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-26 14:54 +0200
Re: Generic read/write function - casting vs union boon <root@localhost.localdomain> - 2016-09-26 19:47 +0200
Re: Generic read/write function - casting vs union Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-26 19:23 +0100
Re: Generic read/write function - casting vs union Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-27 13:10 -0700
Re: Generic read/write function - casting vs union David Thompson <dave.thompson2@verizon.net> - 2016-10-16 18:08 -0400
Re: Generic read/write function - casting vs union Anton Shepelev <anton.txt@gmail.com> - 2016-10-17 23:40 +0300
Re: Generic read/write function - casting vs union albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-11 15:20 +0100
Re: Generic read/write function - casting vs union supercat@casperkitty.com - 2016-12-11 09:38 -0800
csiph-web