Thanks for your comments. I would try to submit on the system.
By the way, I found a work around to avoid the crash which still keep the global struct member alignment to default but add extra parameter before/after the specified struct:
Qt Code:
#pragma pack(push,1) typedef struct receive_data { unsigned short tag; char name[10]; char id[10]; } RECEIVEDATA; #pragma pack(pop)To copy to clipboard, switch view to plain text mode
Remember that packing structures to 1-byte alignment is not cross-platform. It will work on some platforms (eg. Intel-based), but crash completely on others (ARM-based). You should use at least 4-byte packing to prevent this problem. For maximum performance and full compatibility with 64-bit platforms, you should use 8-byte packing.
I think you want to have 1 byte alignment because you want to read from network directly into the structure. It's better to avoid lazyness and do it properly:
Qt Code:
struct RECEIVEDATA { unsigned short tag; char name[10]; char id[10]; }; // assuming 22 bytes //... item.tag = (ba.at(0) << 8) | ba.at(1); memcpy(item.name, ba.constData()+2, 10); memcpy(item.id, ba.constData()+12, 10);To copy to clipboard, switch view to plain text mode
Assuming of course that the endianess of the server sending you the data is the same as the one receiving the data, else you'll need to swap the bytes around in the item.tag line.
Well, a lot of servers run on Intel hardware, and Intel is little endian, so some people like the send the network traffic in little endian too so it's easier. Other people get upset that the standard for network traffic is big endian, etc and complain that logically, little endianess makes more sense and is more logical.
Yeah.... for Microsoft it is also easier to use their own solutions instead of being standard compliant. But that won't make me say it's good that they do that.
I come from a non-intel background (big-endian, by the way), even a non-PC one and I hate people treating "intel based PC" and "computer" as synonyms...
Bookmarks