PDA

View Full Version : byte struct alignment crash?



SamT
18th April 2010, 10:36
hi there,

does anybody encounter the byte alignment issue with QT 4.6.2?
I'd set the 'struct member alignemnt' to 1byte on my compiler environment (VS2008) and the program crashed during runtime. Everything is fine if change back to default (8byte). How can I report this to QT develope team? :confused:

wysota
18th April 2010, 11:51
http://bugreports.qt.nokia.com

SamT
18th April 2010, 12:17
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:



#pragma pack(push,1)
typedef struct receive_data
{
unsigned short tag;
char name[10];
char id[10];
} RECEIVEDATA;
#pragma pack(pop)

squidge
18th April 2010, 13:15
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.

wysota
18th April 2010, 13:48
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:


struct RECEIVEDATA {
unsigned short tag;
char name[10];
char id[10];
}; // assuming 22 bytes
//...
QByteArray ba = socket->read(22);
item.tag = (ba.at(0) << 8) | ba.at(1);
memcpy(item.name, ba.constData()+2, 10);
memcpy(item.id, ba.constData()+12, 10);

squidge
18th April 2010, 18:52
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.

Kumosan
18th April 2010, 22:12
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.

A server not sending data in big-endian is an abomination. :cool:

squidge
18th April 2010, 23:53
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.

wysota
19th April 2010, 00:19
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...