C ++ platform compatibility

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

 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 problems can arise when loading foo on platform 2?

+4
source share
3 answers

When you do this, you need to keep an eye on:

  • Dimensions of the data type ( char is the only one you can trust)
  • Alignment / Padding
  • Byte order
  • Invalid memory reference
  • Floating point
  • ASCII vs EBCDIC ? (Yes seriously?)
  • Maybe others
+8
source

Every kind of problem!

We do not know whether char , int , long or char* tags are the same on different platforms.

And what happened to material d designated?

There may also be indentation between members, which may vary between platforms. Large finite and small-end systems will store bytes of integers and pointers in a different order. If you're really out of luck, there might be a mid-end system.

+12
source

I think you should use this package pragma to make sure there are no indents. otherwise char will have 4 bytes depending on the default padding method.

char * this address pointer may have 32 bits on a 32-bit machine, but 64 bits on a 64-bit machine. So keep the pointer directly because of the nonsense.

The latter is endian.

0
source

All Articles