C++ question: data type sizes?

Nibbie
Posts: 2
Joined: 2008.12
Post: #1
Howdy

Hope you guys don't mind me asking this question here, it's possibly a basic C++ question, but it's giving me a headache.

Basically, I want to use the same data file under Windows and Mac OS X. I thought it'd be fine. I'm using the same code to load the data on both, but I created the datafile under Windows.

Problem is, the data wont load on the mac. It loads the first few bytes of the header part of the file. The header is:
char id[4],
unsigned char
unsigned char
unsigned int
unsigned long -- and here's where it fails. Up til this long all the data is fine, but here the value is 4 in the data file, it comes out as something in the millions.

I'm wondering what could cause this? Would it be the different byte sizes of data types on the 2 systems? Both say int is 4, and so is long, so I don't see why there'd be a problem. And the header is read as one chunk, so I don't get why the first half would be fine, and then the rest stuffed?

It's late, I could just be stupid, and probably am. But any suggestions would be greatly appreciated!

Thanks!

Rob.
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #2
This sounds like a "byte endianness" issue. Search the forums. Smile
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2008.12
Post: #3
Ahhhhh yes. Geez how stupid do I feel? I was just reading about this little/big endian stuff, and I still didn't twig.

I should never have swapped my head for a bag of peanuts, I can't even taste them.

Thanks!

Rob.
Quote this message in a reply
Puzzler183
Unregistered
 
Post: #4
So you know, if you plan to write cross platform code, you might want to do something to the following effect:

Code:
//Integral
typedef signed char int8;
typedef signed short int16;
typedef signed long int32;
typedef signed __int64 int64;
typedef signed int ints;

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned long uint32;
typedef unsigned __int64 uint64;
typedef unsigned int uints;

//Floating point
typedef float real32;
typedef double real64;
typedef long double real80;
typedef real32 real;

Then you change those if your platform changes and you always have guaranteed sizes for your types (assuming you change them properly).
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
<stdint.h> has fixed-size integer typedefs (int32_t, uint64_t, etc).
Quote this message in a reply
Puzzler183
Unregistered
 
Post: #6
Hmm, never saw that before. Good call OSC.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2008.12
Post: #7
Outstanding! You blokes know more than the ranting guy at the bus stop who I previously asked! Smile

Thanks a lot.
Quote this message in a reply
Post Reply