Padding/packing structures
I'm trying to send a struct{} from my server to the client, but when it comes in it's all garbled. I've read that sometimes you need to add the #pragma pack directive, but that hasn't worked; I tried pack(), pack(1, 2, and 4). Another problem might be that the client is on windows, and the server is on linux. Is there a way to send a whole struct as one entity rather than to send each part seperately?
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
If you sort your struct from largest element to smallest, there's a good chance it'll come out the same, if the compiler and CPU use mostly the same packing semantics, byte order, and word sizes.
Thus, this should be pretty safe:
Arrays of elements have the same alignment restrictions as the element itself, usually.
You could also write a program that creates the struct in question, prints out sizeof(struct), and prints out the raw data (in hex) of the memory, run it on both machines, and compare.
Thus, this should be pretty safe:
struct { double x, y, z; long foo; short bar; char baz;};
Arrays of elements have the same alignment restrictions as the element itself, usually.
You could also write a program that creates the struct in question, prints out sizeof(struct), and prints out the raw data (in hex) of the memory, run it on both machines, and compare.
enum Bool { True, False, FileNotFound };
Well in windows the size of my struct is 40, in linux it's 16.
The big difference comes from string. It's 28 in windows, and 4 in linux.
Is there any way to compensate for that much of a difference, or should I not use string?
struct {std::string name;float x, y, z;}
The big difference comes from string. It's 28 in windows, and 4 in linux.
Is there any way to compensate for that much of a difference, or should I not use string?
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Generally speaking you can't simply send structs that contain non-primitives like std::string. You'll either need to send 'name' seperately or repackage them into a send buffer.
The same thing applies to pointers. If your struct contains a pointer you can't simply send it and have things work. You need to send the pointed-to data explicitely.
The same thing applies to pointers. If your struct contains a pointer you can't simply send it and have things work. You need to send the pointed-to data explicitely.
-Mike
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement