Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ platform compatibility

Tags:

c++

An object foo is written to a new file on platform 1 as:

write( file, &myFoo, sizeof(struct foo) );

...and then read on platform 2 using:

read(file, &myFoo, filesize(file) );

The foo object has the following definition:

struct foo
{
    char  a;
    int   b; 
    long  c;
    char* d;
};

What kind of issues might arise when loading foo on platform 2?

like image 227
Chebz Avatar asked Dec 01 '22 08:12

Chebz


1 Answers

Every kind of issue!

We don't know if char, int, long or char* are the same size on different platforms.

And what happened to the stuff d pointed to?

There might also be padding between the members, which could differ between platforms. Big endian and little ending systems would store the bytes of integers and pointers in different order. If you are really unlucky, there might be a middle endian system as well.

like image 57
Bo Persson Avatar answered Dec 05 '22 06:12

Bo Persson