C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Portable types
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Murali
Guest





PostPosted: Sun Oct 02, 2005 1:30 am    Post subject: Portable types Reply with quote



Hi,

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

My question is, why can't this mechanism be made a part of some
standard header so that a common vocabulary can be used. For eg., we
can have predefined types int8_t, uint8_t, int16_t, int32_t etc.
Is this already available in some form? Was this considered during the
standardization process?

Thanks.

Regards,
Murali


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
albrecht.fritzsche
Guest





PostPosted: Sun Oct 02, 2005 4:12 pm    Post subject: Re: Portable types Reply with quote



Murali wrote:
Quote:
Hi,

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

Here the problem already starts - what the language guarantees is that

sizeof(char) == 1 and 1 <= sizeof(the other types)

but to the best of my knowledge that's it (apart from size_t being
sufficiently large). So, in your naming, eg Int8, what does the 8 stands
for? I guess it should mean the number of bits. But this is already
contrary to sizeof(), which returns you the number of bytes since
there is the possibility that on some machines 1 byte = 7 bits or some-
thing completely different (even though I've never met such a machine).

Quote:
My question is, why can't this mechanism be made a part of some
standard header so that a common vocabulary can be used. For eg., we
can have predefined types int8_t, uint8_t, int16_t, int32_t etc.

I guess that several compiler builders already offer you such types,
so, if you know the range of your targetting OSs you might use those.
But, in the end, using your own typedefs is the safest way - then
targetting to a new OS can start as easily as adding the correct
typedefs.

Ali

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Stanimir Kabaivanov
Guest





PostPosted: Sun Oct 02, 2005 4:17 pm    Post subject: Re: Portable types Reply with quote



Hi Murali,
Quote:

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

My question is, why can't this mechanism be made a part of some
standard header so that a common vocabulary can be used. For eg., we
can have predefined types int8_t, uint8_t, int16_t, int32_t etc.
Is this already available in some form? Was this considered during the
standardization process?

Well, according to the C99 standard, all vendors that claim to have C99

compliant tools (compilers) should provide stdint.h header file, that
defines exactly the types you mention.
You can check your compiler standard headers and figure out if its
there. On my system it looks like:

/* 7.18.1.1 Exact-width integer types */
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned uint32_t;
typedef long long int64_t;
typedef unsigned long long uint64_t;

7.18.1.1 (http://dev.unicals.com/papers/c99-draft.html#7.18.1.1) is what
you should be looking for to read the exact text.

Besides there's a good article by Michael Barr that explains reasoning
behind this decision (and drawbacks too Smile ):

http://www.embedded.com/shared/printableArticle.jhtml?articleID=17300092

HTH,
Stanimir Kabaivanov

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Thomas Maeder
Guest





PostPosted: Sun Oct 02, 2005 4:18 pm    Post subject: Re: Portable types Reply with quote

"Murali" <murdesi (AT) yahoo (DOT) com> writes:

Quote:
Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their
code to be portable normally define their own typedefs to obtain
types of required size (for eg., Int8, UInt8, Int16 etc).

My question is, why can't this mechanism be made a part of some
standard header so that a common vocabulary can be used. For eg., we
can have predefined types int8_t, uint8_t, int16_t, int32_t etc. Is
this already available in some form? Was this considered during the
standardization process?

Not exactly Standard, but very portable:

http://www.boost.org/libs/integer/cstdint.htm

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jeff Schwab
Guest





PostPosted: Sun Oct 02, 2005 4:18 pm    Post subject: Re: Portable types Reply with quote

Murali wrote:
Quote:
Hi,

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

My question is, why can't this mechanism be made a part of some
standard header so that a common vocabulary can be used. For eg., we
can have predefined types int8_t, uint8_t, int16_t, int32_t etc.
Is this already available in some form? Was this considered during the
standardization process?

It's standard in C, and available in C++.

http://boost.org/libs/integer/cstdint.htm

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carl Barron
Guest





PostPosted: Sun Oct 02, 2005 4:19 pm    Post subject: Re: Portable types Reply with quote

Murali <murdesi (AT) yahoo (DOT) com> wrote:

Quote:
Hi,

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

My question is, why can't this mechanism be made a part of some
standard header so that a common vocabulary can be used. For eg., we
can have predefined types int8_t, uint8_t, int16_t, int32_t etc.
Is this already available in some form? Was this considered during the
standardization process?

Thanks.

c99 provides stdint.h. and there is motion if not already approved

for a cstdint in c++0x.

see section 7.18 of the 1999 C standard for a definition of the
exact contents of stdint.h.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ivan Vecerina
Guest





PostPosted: Sun Oct 02, 2005 4:20 pm    Post subject: Re: Portable types Reply with quote

"Murali" <murdesi (AT) yahoo (DOT) com> wrote

Quote:
Hi,

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

My question is, why can't this mechanism be made a part of some
standard header so that a common vocabulary can be used. For eg., we
can have predefined types int8_t, uint8_t, int16_t, int32_t etc.
Is this already available in some form? Was this considered during the
standardization process?

Look for <stdint.h> in the C99 standard.
[draft available here: http://www.vmunix.com/~gabor/c/draft.html]

I haven't kept track of it, but this is likely to be incorporated
in some form into the next C++ standard.


Cheers, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Greg Herlihy
Guest





PostPosted: Sun Oct 02, 2005 4:21 pm    Post subject: Re: Portable types Reply with quote


Murali wrote:
Quote:
Hi,

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

My question is, why can't this mechanism be made a part of some
standard header so that a common vocabulary can be used. For eg., we
can have predefined types int8_t, uint8_t, int16_t, int32_t etc.
Is this already available in some form? Was this considered during the
standardization process?

Thanks.

Regards,
Murali

You should be able to find the "exact width" integer types in stdint.h.
The stdint.h header is part of the C99 standard which many C++
compilers support. I would hazard a guess that stdint.h will become
part of the C++ standard at some point; in the meantime I can see no
reason not to include it.

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jack Klein
Guest





PostPosted: Sun Oct 02, 2005 4:21 pm    Post subject: Re: Portable types Reply with quote

On 1 Oct 2005 21:30:30 -0400, "Murali" <murdesi (AT) yahoo (DOT) com> wrote in
comp.lang.c++.moderated:

Quote:
Hi,

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

My question is, why can't this mechanism be made a part of some
standard header so that a common vocabulary can be used. For eg., we
can have predefined types int8_t, uint8_t, int16_t, int32_t etc.
Is this already available in some form? Was this considered during the
standardization process?

Thanks.

Regards,
Murali

Sounds like you want the standard C header <stdint.h>, added to the C
standard in 1999, to be added to the C++ standard. And it almost
certainly will be, as <cstdint>, some day. I would imagine that
almost all C++ compiler ship with a <stdint.h> right now, in fact.

With the possible exception of the integer types with at least 64
bits, you can make a workable <stdint.h> header for an conforming C89
or C++ compiler yourself.

But note that the exact width integer types may not be present. They
are required if and only if an implementation has integer data types
with exactly that width, no padding bits, and using a 2's complement
representation for negative values in the signed versions.

This is actually a very real issue, even if most desktop programmers
don't realize it. DSPs (Digital Signal Processors) are becoming more
widely used all the time, and the newer ones have C and C++ compilers.

I am working right now a TI2812 DSP, which comes with a freestanding
C++ compiler, although I am programming it in C. You can't have a
<stdint.h> header that defines int8_t and uint8_t because no such type
exists on this architecture. It accesses memory in 16 bit chunks, and
the char, short, and int types are all 16 bits.

Likewise there are DSPs where all the integer types are 32 bits.

For maximum portability, you can use the int_least8_t or int_fast8_t,
but each of these will may have more than 8 bits on some
implementations.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jack Klein
Guest





PostPosted: Sun Oct 02, 2005 8:23 pm    Post subject: Re: Portable types Reply with quote

On 2 Oct 2005 12:12:31 -0400, "albrecht.fritzsche"
<albrecht.fritzsche (AT) arcor (DOT) de> wrote in comp.lang.c++.moderated:

Quote:
Murali wrote:
Hi,

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

Here the problem already starts - what the language guarantees is that

sizeof(char) == 1 and 1 <= sizeof(the other types)

but to the best of my knowledge that's it (apart from size_t being
sufficiently large). So, in your naming, eg Int8, what does the 8 stands
for? I guess it should mean the number of bits. But this is already
contrary to sizeof(), which returns you the number of bytes since
there is the possibility that on some machines 1 byte = 7 bits or some-
thing completely different (even though I've never met such a machine).

[snip]

This last bit is incorrect. C++ and C require that a byte, that is a
char, contain at least 8 bits. It can contain more than 8, but never
less. There are both C and C++ compilers for platforms, mostly DSPs
(Digital Signal Processors) where a char contains 16 or 32 bits.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jack Klein
Guest





PostPosted: Sun Oct 02, 2005 8:25 pm    Post subject: Re: Portable types Reply with quote

On 2 Oct 2005 12:17:51 -0400, Stanimir Kabaivanov
<kabaivanov (AT) bulgaria (DOT) com> wrote in comp.lang.c++.moderated:

Quote:
Hi Murali,

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

My question is, why can't this mechanism be made a part of some
standard header so that a common vocabulary can be used. For eg., we
can have predefined types int8_t, uint8_t, int16_t, int32_t etc.
Is this already available in some form? Was this considered during the
standardization process?

Well, according to the C99 standard, all vendors that claim to have C99
compliant tools (compilers) should provide stdint.h header file, that
defines exactly the types you mention.

All C99 conforming compilers must indeed provide the stdint.h header.
Many C and C++ compilers that do not claim C99 conformance provide it
as an extension. But the header is not required to provide the types
the OP asked about.

Quote:
You can check your compiler standard headers and figure out if its
there. On my system it looks like:

/* 7.18.1.1 Exact-width integer types */
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned uint32_t;
typedef long long int64_t;
typedef unsigned long long uint64_t;

Paragraph 3 of "7.18.1.1 Exact-width integer types" states
specifically:

"These types are optional. However, if an implementation provides
integer types with widths of 8, 16, 32, or 64 bits, it shall define
the corresponding typedef names."

There are platforms in existence today, with C and C++ compilers, that
do not provide all of these types, because the processor hardware does
not support them. This is most common with DSPs.

All the other types provided in <stdint.h> are required, but the
exact-width types are not, if they don't exist on the target
processor.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Krakers
Guest





PostPosted: Mon Oct 03, 2005 1:10 pm    Post subject: Re: Portable types Reply with quote

"Murali" <murdesi (AT) yahoo (DOT) com> wrote...
Quote:

Given that in C++ the sizes of fundamental types are implementation
defined (subject to some restrictions), programmers who want their code
to be portable normally define their own typedefs to obtain types of
required size (for eg., Int8, UInt8, Int16 etc).

Exact sizes of integer types are neither required nor sufficient
to write portable code. There are two fundamental issues with
the size of integer variables and portability:

1. Avoiding overflow. That is a matter of minimal (and not exact)
sizes and C/C++ standard guarantees those: char is at least 8,
short 16 and long is at least 32 bits. Int may be used instead of
short, as it will not be smaller.

2. Writing to a file. Here, a popular, but not portable way is to
take an address of a variable and write sizeof variable bytes
to a file. A portable way is to design the file to use either
big-endian or little-endian convention and write (or read) a fixed
number of bytes, combining them into a number. Exact sizes
are not required in this approach - again the variable should
not be smaller, but may be larger than the number of bytes it
needs to hold.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
albrecht.fritzsche
Guest





PostPosted: Mon Oct 03, 2005 1:14 pm    Post subject: Re: Portable types Reply with quote

Jack Klein wrote:
Quote:
On 2 Oct 2005 12:12:31 -0400, "albrecht.fritzsche"
[email]albrecht.fritzsche (AT) arcor (DOT) de[/email]> wrote in comp.lang.c++.moderated:
since
there is the possibility that on some machines 1 byte = 7 bits or some-
thing completely different (even though I've never met such a machine).

This last bit is incorrect. C++ and C require that a byte, that is a
char, contain at least 8 bits.

Yes, I stand corrected - "A byte is at least large enough to contain any
member of the basic execution character set and is composed of a
contigous sequence of bits, the number of which is implementation-
defined." (1.7.1) That's exactly what makes reading the Standard a
(black) science in its own - why can't there be a note like: there are
X members of that very character set and hence a byte requires at least
Y bits.

Cheers,
Ali

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carl Barron
Guest





PostPosted: Mon Oct 03, 2005 1:17 pm    Post subject: Re: Portable types Reply with quote

Jack Klein <jackklein (AT) spamcop (DOT) net> wrote:

Quote:

Paragraph 3 of "7.18.1.1 Exact-width integer types" states
specifically:

"These types are optional. However, if an implementation provides
integer types with widths of 8, 16, 32, or 64 bits, it shall define
the corresponding typedef names."

There are platforms in existence today, with C and C++ compilers, that
do not provide all of these types, because the processor hardware does
not support them. This is most common with DSPs.

All the other types provided in <stdint.h> are required, but the
exact-width types are not, if they don't exist on the target
processor.
At least if you use have stdint.h and use it, then any non-existant

tyoe will be a compiler error and not some wild runtime bug, that only
surfaces occasionally depending on the data.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Niklas Matthies
Guest





PostPosted: Tue Oct 04, 2005 1:43 pm    Post subject: Re: Portable types Reply with quote

On 2005-10-03 13:14, albrecht.fritzsche wrote:
Quote:
Jack Klein wrote:
On 2 Oct 2005 12:12:31 -0400, "albrecht.fritzsche" wrote:
:
since there is the possibility that on some machines 1 byte = 7
bits or some- thing completely different (even though I've never
met such a machine).

This last bit is incorrect. C++ and C require that a byte, that is
a char, contain at least 8 bits.

Yes, I stand corrected - "A byte is at least large enough to contain
any member of the basic execution character set and is composed of a
contigous sequence of bits, the number of which is implementation-
defined." (1.7.1) That's exactly what makes reading the Standard a
(black) science in its own - why can't there be a note like: there
are X members of that very character set and hence a byte requires
at least Y bits.

Because that's not the reason. The basic execution character set has
only 95 required characters (or 96 if you count the null character),
which would fit into 7 bits. Instead it follows from the minimum value
of CHAR_BIT.

-- Niklas Matthies

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.