tags:

views:

85

answers:

4

Hi,

I had been doing some file IO in a project i am currently working on and so far I have been reading in a whole block of data using the following fast and convenient method:

struct Header { ... };
class Data { ... };
// note that I have not used compiler directives to pack/align/order bytes
// partly because I don't know how to.

Header _header;
Data _data;
std::ifstream fin(filename);
fin.read((char*)&_header, sizeof(Header));
fin.read((char*)&_data, sizeof(Data));
fin.close();

My question is whether it is ok to assume the bytes are aligned and order in the same way for every compiler and every different computer?

For example, if I take the Header struct and compile a client program, on linux, and a server program on windows. Are the bytes in the same order such that there will be no issues receiving and sending both ways?

+6  A: 

No, that's not guaranteed at all. There is a specific network byte order and as far as I know, both WinAPI and POSIX provide local to network translation functions. In addition, the alignment you can control with compiler directives. But you have to explicitly take care of both of these things.

DeadMG
+2  A: 

Well, in the general case of course it isn't. Some platforms have different byte orderings (endianness) than others.

Also, on 64-bit platforms some common integer types (like size_t) are liable to be different sizes than you expect. All C guarantees is that sizeof(long)>=sizeof(short)>=sizeof(char), so there could even be a perverse platform out there where long, short, and char are all the same size.

Realistically, if both your platforms are Intel and both your OS's are 32-bit (or both are 64-bit) you are probably OK. Still it would be better to look into your compiler's directives for nailing alignment and ordering down a bit better. Sadly, C and C++ are not (yet) nearly as good at doing this as Ada. You could use a standard encoding like ASN.1 to fix this problem, but ASN.1 is a cure that is almost always worse than the disease.

T.E.D.
A: 

You are using sizeof operator. So it should be safe, at least source level compatible.

Madhava Gaikwad
+4  A: 

This is a solved problem, avoid re-inventing that wheel. XML is the lingua franca of machines talking to each other over the internet. It's chatty, if bandwidth is a concern then look for Google's protobuf. It has many language bindings, C++ is well supported.

Hans Passant
It's also worth mentioning JSON.
Magnus Hoff
Although my test platform is another PC, my end target platform is a microchip processor. Not sure if it will work... probably not?
Jake