I
Ian Collins
Kelsey said:Indeed. And yet you can't rely on that, because the new int types may not
exist *at all* on the new platform.
Then you either add them, or give up as you can't port the code.
Kelsey said:Indeed. And yet you can't rely on that, because the new int types may not
exist *at all* on the new platform.
Kelsey said:[snips]
You're still missing the point. If I want to write code that is only
portable to systems that have a 32-bit integer type, then the easiest
way to get that data type is to use int32_t.
Which deals with one type. You can, of course, guarantee that this system
_also_ has, oh, 8 bit integer types, right? No? So all that code using
uint8_t now has to be scrapped or rewritten, because the types simply
aren't available.
By the time you get to the point where all these new types make sense,
you've essentially restricted C to existing only on 32-bit x86 machines or
close equivalents. Hardly sensible.
Nor can you rely on the new types, as they may well simply not exist on
a given implementation. Yes, and?
Then you either add them, or give up as you can't port the code.
Then you have the concept of standard conforming, non-portable code.Richard said:Ian Collins said:
No, you write the code so as not to need them. In other words, you write
your code *not* to rely on exact size.
Kelsey is absolutely right. If a type isn't guaranteed to be provided by
the implementation, one cannot use it in code that is required to be
portable to arbitrary implementations.
Richard Heathfield wrote:
Then you have the concept of standard conforming, non-portable code.
But availability of fixed with types isn't one of them, it has alwaysRichard said:So one must exercise care even in C90 (or accept that one's code is not
portable to certain kinds of implementation). C99 has all of the C90
portability issues, and adds a whole bunch of new ones.
Al Balmer said:They are not guaranteed to be the same size on different platforms,
and sometimes you want exact size.
But availability of fixed wi[d]th types isn't one of them,Richard said:So one must exercise care even in C90 (or accept that one's code is
not portable to certain kinds of implementation). C99 has all of the
C90 portability issues, and adds a whole bunch of new ones.
it has always been an issue.
All C99 does is provide a standardised set of names.
If your code is processing a stream of say 16 bit data, you can either
assume the existence of a 16 (or 8) bit type, or come up with
something horribly complex to cope with systems where integer types
are a multiple of something other than eight.
Kelsey said:[snips]
This newsgroup notwithstanding, the reality is that there are a lot of
people that choose to write "unportable" code for various purposes.
Sure, I've done so myself. And when doing so, I'll happily use the fact
that on implementation X, char is 8 bits, int 16, long 32 or what have you
and not need those pesky int32_t things at all.
Remember, they have to exist _if_ the implementation already has such
types, meaning that if they exist, you *already* have perfectly
functional, though non-portable, options, making the int types completely
irrelevant.
Personally I think it would be better if it required providing the
intN_t types for all values of N for which it has a type with no
padding bits (and two's complement for the signed versions). I can't see
that it would be much work for the implementer.
How do you know that your type has the same endianness as the fileTheir unportability is, in this case, a feature. Suppose that you want
an exactly 32-bit unsigned type, for example to agree with a file
format.
If your C99 implementation has a type of the kind you want, you
can rely on uint32_t being there. If uint32_t is not available, that
implementation is simply never going to give you what you want. In C90,
you'd have to put that kind of type behind conditional compilation
#ifdefs, one for each platform you want, _and_ hope that your compiler
never introduces padding bits behind your back; or you have to surround
every use of your type with &0xFFFFFFFF and so on. In C99, all you have
to do is check whether your compiler complains about uint32_t not
existing.
Kelsey Bjarnason said:[snips]
On Tue, 11 Sep 2007 12:08:08 +0000, Richard Bos wrote:
Their unportability is, in this case, a feature. Suppose that you want
an exactly 32-bit unsigned type, for example to agree with a file
format. If your C99 implementation has a type of the kind you want, you
can rely on uint32_t being there. If uint32_t is not available, that
implementation is simply never going to give you what you want.
No, but if I were to use, say, an unsigned long I'd know I was using an
unsigned type at least 32 bits long, and more importantly, I'd know the
type was going to exist, period.
So now I use a uint32_t and my code fails on compiler X because the type
simply does not exist.
Then, neither does the 32-bit unsigned long. If your requirement is
for a type *at least* 32 bits, rather than exactly 32 bits, you would
use int_least32_t.
Exactly the point. We *already* have, without the new int types, a
functional set of types which are _at least_ n bits wide.
They have all the utility of void main() - they'll work, somewhere, on some
compiler, for some definition of "work", but you can't rely on them
working _at all_ on an arbitrary compiler.
CBFalconer said:Richard said:CBFalconer said:Flash Gordon wrote:
Personally I think it would be better if it required providing the
intN_t types for all values of N for which it has a type with
no padding bits (and two's complement for the signed versions). I
can't see that it would be much work for the implementer.
Ugh. You don't really want to hear my opinion about those useless
non-portable foolishnesses.
Their unportability is, in this case, a feature. Suppose that you want
an exactly 32-bit unsigned type, for example to agree with a file
format. If your C99 implementation has a type of the kind you want, you
can rely on uint32_t being there. If uint32_t is not available, that
implementation is simply never going to give you what you want. In C90,
you'd have to put that kind of type behind conditional compilation
#ifdefs, one for each platform you want, _and_ hope that your compiler
never introduces padding bits behind your back; or you have to surround
every use of your type with &0xFFFFFFFF and so on. In C99, all you have
to do is check whether your compiler complains about uint32_t not
existing.
Lets work with your example. The padding bits don't matter,
because they don't show up in the actual values, they are limited
to catching such things as use of uninitialized data, or ECC error
correction, etc. So we assume the file system is capable of
writing 36 bit values, as a result of having CHAR_BIT == 9 in the
system. But you are writing software using values that have to
work on the usual system with CHAR_BIT == 8. unsigned long always
has at least 32 bits available, so you use that type (or
uint32_t). The padding doesn't matter, as stated above (most
systems with ECC today really have a 72 bit word, which is what is
really being read and written from/to memory).
You have less worries about overflows with the 36 bit system.
Using unsigned values, truncation down to 32 bits doesn't matter,
the truncated result will be identical to the value on the 32 bit
system. But it is generally easier to detect 32 bit overflow with
a 36 bit system. You won't be doing any such.
What you will be doing is reading and writing those values to the
file or i/o system. If the peripheral or file only handles 32 bit
wide values, it will just ignore the higher order bits. This just
gives the identical result to what would have happened on the 32
bit system. Everything is free!!.
What about input from the device/file? The high order bits will be
zeroed, we assume, because the hardware has some pins tied to
ground, or the equivalent. In comes a value identical the the
value on the 32 bit system. Once more, everything is free.
Notice there has never been any need to mask off fields. Now shoot
me down.
Your copy of the draft is irrelevant.Kelsey Bjarnason said:[snips]
If you just want integer types of specified sizes, take a look at the
<stdint.h> header, which defines int8_t, int16_t, etc.
My copy of the draft says
Kelsey Bjarnason said:Indeed. And yet you can't rely on that, because the new int types may not
exist *at all* on the new platform.
Your copy of the draft is irrelevant.Kelsey Bjarnason said:[snips]
If you just want integer types of specified sizes, take a look at
the <stdint.h> header, which defines int8_t, int16_t, etc.
My copy of the draft says
It is a DRAFT if you are going to mess around with the compiler you
need the standard itself. NOT a draft.
Richard Heathfield said:Chris Hills said:
Your copy of the draft is irrelevant.Kelsey Bjarnason said:[snips]
On Thu, 06 Sep 2007 11:51:57 -0700, Keith Thompson wrote:
If you just want integer types of specified sizes, take a look at
the <stdint.h> header, which defines int8_t, int16_t, etc.
My copy of the draft says
It is a DRAFT if you are going to mess around with the compiler you
need the standard itself. NOT a draft.
Is it your contention that the final Standard differs from the draft in
this regard?
Specifically, Kelsey said: "My copy of the draft says that
intN_t types are optional, an implementation is not required to provide
them." As far as I'm aware,
this remains true in the final Standard. I
refer you specifically to 7.18(4): "An implementation shall provide
those types described as ``required'', but need not provide any of the
others (described as ``optional'')."
It seems to me that your objection is nebulous.
But availability of fixed with types isn't one of them
, it has always
been an issue. All C99 does is provide a standardised set of names.
If your code is processing a stream of say 16 bit data, you can either
assume the existence of a 16 (or 8) bit type
In which case, the program you want can't be easily written for that
platform anyway.
Yes. And _exactly_ n bits wide?
void main() adds no functionality. Exact-width types do.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.