[Qt-interest] QByteArray::fromRawData() oddity...

BRM bm_witness at yahoo.com
Wed Nov 18 00:39:24 CET 2009


Question:

Given the following structure:

struct exampleStructure
{
     quint32 command;
        quint32 other;
        quint32 picnic;
     
        void Pack()
                {
                command = htonl(command);
                other = htonl(other);
                picnic = htonl(picnic);
                }
        void UnPack()
                {
                command = ntohl(command);
                other = ntohl(other);
                picnic = ntolh(picnic);
             }
};

Should the following bits of code produce the same result:

struct exampleStructure test;
memset(&test,0,sizeof(struct exampleStructure);

// bit #1:
QByteArray data = QByteArray::fromRawData((char*)&test,sizeof(struct exampleStructure));

// bit #2:
QByteArray data2((char*)&test,sizeof(struct exampleStructure));

I just spent most of the day figuring out an issue that tracked down to that difference - where #2 worked while #1 did not.
I would have thought they'd produce the same result (e.g. data2 should be the same as data).

Using Qt 4.5.1 in my program under Linux with g++ 4.2.4 - Ubuntu 4.2.4-1ubuntu4.

If I'm mistaken okay - but I'd really like to know.

TIA,

Ben



More information about the Qt-interest-old mailing list