Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #62317

Re: determines if it is a signed type

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: determines if it is a signed type
Date 2015-05-19 20:36 +0100
Organization A noiseless patient Spider
Message-ID <871ticjr51.fsf@bsb.me.uk> (permalink)
References (14 earlier) <ec76f4dc-48df-4003-b045-23dbd7c5cec5@googlegroups.com> <87d21wk6k4.fsf@bsb.me.uk> <c82653e9-61b8-4591-9d81-dba647009d3b@googlegroups.com> <877fs4k0si.fsf@bsb.me.uk> <b7d0b707-df27-42df-bb8a-27c6d1702178@googlegroups.com>

Show all headers | View raw


jadill33@gmail.com writes:

I'm just going to restart this from your example because the intervening
stuff does not help as far as I can see.

> On Tuesday, May 19, 2015 at 12:08:36 PM UTC-4, Ben Bacarisse wrote:
>> jadill..@gmail.com writes:
>> 
>> > On Tuesday, May 19, 2015 at 10:04:08 AM UTC-4, Ben Bacarisse wrote:
>> >> jadill..@gmail.com writes:
<snip>
>> >> > Yeah, one of the strongest motivations to use stdint.h is reading/writing
>> >> > binary protocols in files/sockets.  You'll be hard pressed to avoid
>> >> > problems on multiple platforms without using some mechanism for
>> >> > defining fixed width types, and stdint.h is well, the standard way
>> >> > of defining the names of those types.

<snip>
> Sure, here is what I use for reading 32-bit unsigned integers in little
> endian format from a file.
>
> \code snippet
> bool file_read_uint32_le( void *p, FILE *stream )
> {
>   uint8_t buf[sizeof (uint32_t)];
>   size_t bytes_read;
>
>   /* argument error handling */
>
>   bytes_read = fread( buf, 1, sizeof (buf), stream );
>
>   if ( bytes_read == sizeof (uint32_t) ) {
>     *((uint32_t*)p) = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
>   }
>   else
>   {
>     if ( bytes_read > 0 ) {
>       fseek( stream, -(long int)bytes_read, SEEK_CUR );
>     }
>
>     return false;
>   }
>
>   return true;
> }
> \endcode
>
> All I'm saying is that if instead of uint32_t I use a basic type, as
> in unsigned int, unsigned long, I can't guarantee that the code will
> work on platforms I care about.

The use of types from stdint.h buys you nothing other than to limit the
code to those platforms that have uint32_t.  The use of these types
might even prevent readers from seeing the non-portable assumptions
you've made about the implementation's int type and the possible
undefined behaviour of the shifts.  (The argument -- in the reader's
head -- being that since the author is using those special types the
code must be a written to be portable.)

You could limit the damage caused by stdint.h by using only uint8_t, but
even then there will be a few odd systems where using char and unsigned
long will work where uint8_t is not available.  Obviously I am not
talking about s/uint8_t/char/ and s/uint32_t/unsigned long/.  The code
must be written to work with these changes, but the re-writes are not
great.

There are quote a few point I'd want to bring up if reviewing this code
but I don't think that's the point here.  The question is whether the
stdint.h types have helped or not, I don't think they have here.

> Maybe there are problems with this
> example on certain incarnations of uint8_t and uint32_t, but I'm not
> that knowledgeable to know for sure.
>
> What I'm trying to say about format is that often times the file format
> is independent of the machine that you are operating on, like a pcapng
> for storing packet traffic.  If I use unsigned long to read a 32-bit
> integer inside that file, I can run into problems using unsigned long
> specifically when the platform assigns unsigned long to an unsigned
> integer of 64-bits.

I made a point to exclude those simple cases where you can assume the
representation.  My point was about cases like the example above where
you can't just fread and fwrite an object.  If you have to shuffle the
bits, you might as well shuffle them into the most universal type that
can hold the value (unsigned long in the case above).

> Hopefully I made it a bit clearer?

Yes, clearer, but I remain un-convinced!  I keep seeing code with
uint32_t in it which would be more portable without it.  Not that it
usually matters, since odd machines without uint32_t are rare, and the
authors of such code almost certainly don't care about those that do
still exist.

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

determines if it is a signed type asetofsymbols@gmail.com - 2015-05-17 10:07 -0700
  Re: determines if it is a signed type Richard Heathfield <rjh@cpax.org.uk> - 2015-05-17 18:12 +0100
    Re: determines if it is a signed type asetofsymbols@gmail.com - 2015-05-17 10:34 -0700
      Re: determines if it is a signed type asetofsymbols@gmail.com - 2015-05-17 11:09 -0700
      Re: determines if it is a signed type Kusalananda <kusalananda@example.com> - 2015-05-17 18:22 +0000
        Re: determines if it is a signed type asetofsymbols@gmail.com - 2015-05-17 11:31 -0700
          Re: determines if it is a signed type Kusalananda <kusalananda@example.com> - 2015-05-17 19:59 +0000
  Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-17 13:12 -0700
  Re: determines if it is a signed type Morris Dovey <mrdovey@iedu.com> - 2015-05-17 19:08 -0500
    Re: determines if it is a signed type Morris Dovey <mrdovey@iedu.com> - 2015-05-17 19:46 -0500
    Re: determines if it is a signed type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-18 01:59 +0100
      Re: determines if it is a signed type Morris Dovey <mrdovey@iedu.com> - 2015-05-17 20:10 -0500
      Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-18 00:37 -0700
        Re: determines if it is a signed type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-18 21:15 +0100
          Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-18 15:15 -0700
        Re: determines if it is a signed type glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-18 21:51 +0000
          Re: determines if it is a signed type James Kuyper <jameskuyper@verizon.net> - 2015-05-18 18:00 -0400
      Re: determines if it is a signed type glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-18 14:37 +0000
        Re: determines if it is a signed type jacobnavia <jacob@jacob.remcomp.fr> - 2015-05-18 17:09 +0200
          Re: determines if it is a signed type glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-18 18:14 +0000
    Re: determines if it is a signed type Richard Heathfield <rjh@cpax.org.uk> - 2015-05-18 06:34 +0100
      Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-18 00:43 -0700
        Re: determines if it is a signed type Richard Heathfield <rjh@cpax.org.uk> - 2015-05-18 13:56 +0100
          Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-18 08:00 -0700
            Re: determines if it is a signed type Richard Heathfield <rjh@cpax.org.uk> - 2015-05-18 20:35 +0100
              Re: determines if it is a signed type jacobnavia <jacob@jacob.remcomp.fr> - 2015-05-18 23:08 +0200
                Re: determines if it is a signed type Richard Heathfield <rjh@cpax.org.uk> - 2015-05-18 22:32 +0100
                Re: determines if it is a signed type jacobnavia <jacob@jacob.remcomp.fr> - 2015-05-19 00:44 +0200
                Re: determines if it is a signed type Richard Heathfield <rjh@cpax.org.uk> - 2015-05-19 00:05 +0100
                Re: determines if it is a signed type jacobnavia <jacob@jacob.remcomp.fr> - 2015-05-19 01:43 +0200
                Re: determines if it is a signed type Richard Heathfield <rjh@cpax.org.uk> - 2015-05-19 01:08 +0100
                Re: determines if it is a signed type "Bartc" <bartc@freeuk.com> - 2015-05-19 11:33 +0100
                Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-19 08:15 -0700
                Re: determines if it is a signed type "Bartc" <bartc@freeuk.com> - 2015-05-19 19:36 +0100
                Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-19 11:55 -0700
                Re: determines if it is a signed type jacobnavia <jacob@jacob.remcomp.fr> - 2015-05-19 11:39 +0200
                Re: determines if it is a signed type "Bartc" <bartc@freeuk.com> - 2015-05-19 11:44 +0100
                Re: determines if it is a signed type jacobnavia <jacob@jacob.remcomp.fr> - 2015-05-19 12:11 +0200
                Re: determines if it is a signed type jadill33@gmail.com - 2015-05-19 05:45 -0700
                Re: determines if it is a signed type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-19 15:03 +0100
                Re: determines if it is a signed type jadill33@gmail.com - 2015-05-19 08:46 -0700
                Re: determines if it is a signed type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-19 17:08 +0100
                Re: determines if it is a signed type jadill33@gmail.com - 2015-05-19 09:52 -0700
                Re: determines if it is a signed type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-19 20:36 +0100
                Re: determines if it is a signed type jadill33@gmail.com - 2015-05-20 07:40 -0700
                Re: determines if it is a signed type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-20 23:12 +0100
                Re: determines if it is a signed type jadill33@gmail.com - 2015-05-21 13:52 -0700
                Re: determines if it is a signed type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-21 22:48 +0100
                Re: determines if it is a signed type jadill33@gmail.com - 2015-05-22 05:50 -0700
                Re: determines if it is a signed type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-22 23:14 +0100
                Re: determines if it is a signed type jadill33@gmail.com - 2015-05-26 14:31 -0700
                Re: determines if it is a signed type Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-22 23:32 -0700
                Re: determines if it is a signed type David Brown <david.brown@hesbynett.no> - 2015-05-20 12:26 +0200
                Re: determines if it is a signed type Richard Heathfield <rjh@cpax.org.uk> - 2015-05-19 15:28 +0100
                Re: determines if it is a signed type raltbos@xs4all.nl (Richard Bos) - 2015-05-23 10:37 +0000
              Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-18 15:21 -0700
                Re: determines if it is a signed type Richard Heathfield <rjh@cpax.org.uk> - 2015-05-19 00:04 +0100
                Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-19 08:03 -0700
                Re: determines if it is a signed type David Brown <david.brown@hesbynett.no> - 2015-05-19 08:47 +0200
                Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-19 08:07 -0700
          Re: determines if it is a signed type James Kuyper <jameskuyper@verizon.net> - 2015-05-18 11:28 -0400
    Re: determines if it is a signed type Keith Thompson <kst-u@mib.org> - 2015-05-18 00:40 -0700

csiph-web