PDA

View Full Version : Typedef quint16 on ARM and Win32



sgrasic
15th May 2006, 00:48
I am using qint type definitions in my applications, but I have a serius problems with the croosplatform compitability.

The structure that I use:

struct DatagramHeader
{
quint8 version;
quint16 lenght;
quint16 checkSum;
}

The problem is that sizeof(DatagramHeader) prints out different value (from Win32) when used on ARM.

Why???

Regards,
Samo

jacek
15th May 2006, 10:53
Probably the problem is not in qints, but in alignment. On normal 32-bit system that structure most likely will look like this:
| version |xxxxxxxxx| length |
| checksum |xxxxxxxxx|xxxxxxxxx|but on x86 it can be also:
| version | length | chec |
| ksum |

If you rely on the order and position of structure elements, you should force compiler to pack the structure. If you use GCC, try:
struct DatagramHeader
{
...
} __attribute__ ((packed));

sgrasic
16th May 2006, 22:24
Thank you for fast reply....

I am using this structures to create a datagram header. I add it like this:

//Adding Header to the datagram
//Setting up variables
struct DatagramHeader header;
int size=sizeof(DatagramHeader);
//Preparing heder
QByteArray newDatagram = QByteArray::fromRawData((char *)&header, sizeof(DatagramHeader));
//Appending data
newDatagram.append(datapacket.data);

And reading from the datagram:
//Cheching the datagram
struct DatagramHeader* header;
header= (DatagramHeader*) datapacket.data.constData();

Is there a better way to do it?

Regards,
Samo

jacek
16th May 2006, 22:27
You might try the QDataStream class.