Choosing data types

I noticed SDL uses alot of defined data types like Uint8 and stuff where
the exact size is defined. When do people suggest that these be used over
the standard datatypes like int and long long? I can’t think of many
datatypes really varying between platforms other than 64bit datatypes.

http://www.mongeese.org

int is usually 16 bits on 16 bit platforms, 32 bits on 32 bit
platforms and 64 bits on 64 bit platforms… There have been some
confusion with early 32 and 64 bit compilers WRT int32 and int64 as
well, so one shouldn’t really rely on anything if the code really is
to be portable.

Besides, type names that explicitly tell the size can help to
eliminate some general confusion when it comes to protocols and file
formats.

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Monday 22 January 2001 05:02, Garrett Banuk wrote:

I noticed SDL uses alot of defined data types like Uint8 and stuff
where the exact size is defined. When do people suggest that these
be used over the standard datatypes like int and long long? I can’t
think of many datatypes really varying between platforms other than
64bit datatypes.

I noticed SDL uses alot of defined data types like Uint8 and stuff where
the exact size is defined. When do people suggest that these be used over
the standard datatypes like int and long long? I can’t think of many
datatypes really varying between platforms other than 64bit datatypes.

it’s partly for clearly stating intent in the code, and partly for
portability and future-safeness. the most common models in use are LP64
(longs and pointers are 64 bits), ILP64 (ints, longs, and pointers are 64
bits), and ILP32 (ints, longs and pointers are 32 bits). Historically the
sizes have varied a lot and it is prudent to assume they may change again

there’s a lot of over-use of these types in the SDL sources, and this may
be changed in a future release. unless you really need a variable of a
certain size, use the standard C types (int, unsigned int, etc)

Well you don’t want to overuse data types with explicit sizes as each
machine is optimized to use data types of a certain size. ie, forcing
16bit ints on a 32bit machine will really slow things down.

http://www.mongeese.orgOn Mon, 22 Jan 2001, David Olofson wrote:

On Monday 22 January 2001 05:02, Garrett Banuk wrote:

I noticed SDL uses alot of defined data types like Uint8 and stuff
where the exact size is defined. When do people suggest that these
be used over the standard datatypes like int and long long? I can’t
think of many datatypes really varying between platforms other than
64bit datatypes.

int is usually 16 bits on 16 bit platforms, 32 bits on 32 bit
platforms and 64 bits on 64 bit platforms… There have been some
confusion with early 32 and 64 bit compilers WRT int32 and int64 as
well, so one shouldn’t really rely on anything if the code really is
to be portable.

Besides, type names that explicitly tell the size can help to
eliminate some general confusion when it comes to protocols and file
formats.

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -’

I noticed SDL uses alot of defined data types like Uint8 and stuff where
the exact size is defined. When do people suggest that these be used over
the standard datatypes like int and long long? I can’t think of many
datatypes really varying between platforms other than 64bit datatypes.

Precisely. You’d obviously want your new program to be easily portable to
an Itanium or Alpha, right? That’s the main reason why SDL uses these
explicit types, and everyone is encouraged to use these data types
wherever they are needed. I remember what it was like porting C code from
16-bit Turbo C to 32-bit GCC, and rewriting code designed for GCC back to
the 16-bit ELKS compiler. Lots of subtle bugs fly back and forth, because
you made assumptions on what size your ints were.On Sun, 21 Jan 2001, Garrett Banuk wrote:


Rafael R. Sevilla <@Rafael_R_Sevilla> +63 (2) 4342217
ICSM-F Development Team, UP Diliman +63 (917) 4458925
OpenPGP Key ID: 0x0E8CE481

Rafael R. Sevilla wrote:

Lots of subtle bugs fly back and forth, because
you made assumptions on what size your ints were.

I have been working with TMS320C3x DSP’s for the past 10 years and they use
pure 32-bit memory. All types (char, int, long, float, double) are 32-bits.
And for good measure, the “float” type is not IEEE and some compilers treat
"long double" as a special 40-bit type (8-bit exponent, 32-bit mantissa)
which actually uses two words (64-bits) of memory. If that doesn’t mess up
some porting assumptions, nothing does! :slight_smile:

  • Randi

Regimental Command
Generic Armored Combat System
http://regcom.sourceforge.net

many compilers come with a header file which
contains the sizes of the types, I can’t remember
what its called but I’d imagine gcc has something
like this.

om> ----- Original Message -----

From: Randi J. Relander [mailto:rjrelander@users.sourceforge.net]
Sent: Tuesday, January 23, 2001 3:22 PM
To: sdl at lokigames.com
Subject: [SDL] Re: Choosing data types

Rafael R. Sevilla wrote:

Lots of subtle bugs fly back and forth, because
you made assumptions on what size your ints were.

I have been working with TMS320C3x DSP’s for the past 10 years and they use
pure 32-bit memory. All types (char, int, long, float, double) are 32-bits.
And for good measure, the “float” type is not IEEE and some compilers treat
"long double" as a special 40-bit type (8-bit exponent, 32-bit mantissa)
which actually uses two words (64-bits) of memory. If that doesn’t mess up
some porting assumptions, nothing does! :slight_smile:

  • Randi

Regimental Command
Generic Armored Combat System
http://regcom.sourceforge.net

I guess the system defined types are best used when you plan on only
using small values, like if you never plan on using a value higher than
2^16 then just using the ‘int’ type would be fine since almost all
machines now adays suppose 16bits ints or higher. Or if accuracy of your
floats is only needed to a certain precision then ‘double’ is fine. Using
the machine defined types would be more optimal than defining your own
size on all machines.

http://www.mongeese.orgOn Tue, 23 Jan 2001, Randi J. Relander wrote:

Rafael R. Sevilla wrote:

Lots of subtle bugs fly back and forth, because
you made assumptions on what size your ints were.

I have been working with TMS320C3x DSP’s for the past 10 years and they use
pure 32-bit memory. All types (char, int, long, float, double) are 32-bits.
And for good measure, the “float” type is not IEEE and some compilers treat
"long double" as a special 40-bit type (8-bit exponent, 32-bit mantissa)
which actually uses two words (64-bits) of memory. If that doesn’t mess up
some porting assumptions, nothing does! :slight_smile:

  • Randi

Regimental Command
Generic Armored Combat System
http://regcom.sourceforge.net

many compilers come with a header file which
contains the sizes of the types, I can’t remember
what its called but I’d imagine gcc has something
like this.

Many do, and the latest C standard (C99) mandates <stdint.h>. Since we can’t
rely on these being present, the best solution is to provide our own typedefs

many compilers come with a header file which
contains the sizes of the types, I can’t remember
what its called but I’d imagine gcc has something
like this.

limits.h. It’s part of Standard C. However, it’s definitely more work to
have to check the definitions in the header file yourself; SDL already
uses this header file to come up with the definitions for Uint32 and its
bretheren.On Tue, 23 Jan 2001, Oisin Mulvihill wrote:


Rafael R. Sevilla <@Rafael_R_Sevilla> +63 (2) 4342217
ICSM-F Development Team, UP Diliman +63 (917) 4458925
OpenPGP Key ID: 0x0E8CE481

[…]

int is usually 16 bits on 16 bit platforms, 32 bits on 32 bit
platforms and 64 bits on 64 bit platforms… There have been some
confusion with early 32 and 64 bit compilers WRT int32 and int64
as well, so one shouldn’t really rely on anything if the code
really is to be portable.

Besides, type names that explicitly tell the size can help to
eliminate some general confusion when it comes to protocols and
file formats.

Well you don’t want to overuse data types with explicit sizes as
each machine is optimized to use data types of a certain size. ie,
forcing 16bit ints on a 32bit machine will really slow things down.

Right, so don’t use these types unless size really matters - but do
use them when designing file formats and protocols. :slight_smile:

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Monday 22 January 2001 16:39, Garrett Banuk wrote: