Results 1 to 9 of 9

Thread: byte struct alignment crash?

  1. #1
    Join Date
    Oct 2007
    Posts
    32
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default byte struct alignment crash?

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: byte struct alignment crash?


  3. #3
    Join Date
    Oct 2007
    Posts
    32
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: byte struct alignment crash?

    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:
    1. #pragma pack(push,1)
    2. typedef struct receive_data
    3. {
    4. unsigned short tag;
    5. char name[10];
    6. char id[10];
    7. } RECEIVEDATA;
    8. #pragma pack(pop)
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: byte struct alignment crash?

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: byte struct alignment crash?

    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:
    1. struct RECEIVEDATA {
    2. unsigned short tag;
    3. char name[10];
    4. char id[10];
    5. }; // assuming 22 bytes
    6. //...
    7. QByteArray ba = socket->read(22);
    8. item.tag = (ba.at(0) << 8) | ba.at(1);
    9. memcpy(item.name, ba.constData()+2, 10);
    10. memcpy(item.id, ba.constData()+12, 10);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: byte struct alignment crash?

    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.

  7. #7
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: byte struct alignment crash?

    Quote Originally Posted by fatjuicymole View Post
    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.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: byte struct alignment crash?

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: byte struct alignment crash?

    Quote Originally Posted by fatjuicymole View Post
    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...

Similar Threads

  1. Using Multi-Byte in place of Unicode
    By TheDuncan in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2010, 16:58
  2. Converting from char to byte
    By Ferric in forum Newbie
    Replies: 2
    Last Post: 8th January 2010, 00:27
  3. Byte shifting in C
    By tntcoda in forum General Programming
    Replies: 3
    Last Post: 14th November 2008, 22:40
  4. Two Byte character Support
    By rajveer in forum General Programming
    Replies: 10
    Last Post: 25th October 2008, 00:29
  5. convert BYTE* to QString
    By tpf80 in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2007, 10:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.