Results 1 to 5 of 5

Thread: [SOLVED] quint16 weirdness

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face [SOLVED] quint16 weirdness

    Or it's me who's finally gone bananas!

    I have this piece of (testing) code:

    Qt Code:
    1. #include <iostream>
    2. #include <stdio.h>
    3.  
    4. #include <QCoreApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication app(argc, argv);
    9.  
    10. unsigned int i;
    11.  
    12. typedef struct
    13. {
    14. quint16 response_id;
    15. quint32 status_change_id;
    16. bool result_code; // true if sucessful, false otherwise
    17. quint8 reserved[3]; /* Set to 0 */
    18. } receipt_data_type;
    19.  
    20.  
    21. receipt_data_type *receipt = new receipt_data_type;
    22. receipt->response_id = 0x0812;
    23. receipt->status_change_id = 1;
    24. receipt->result_code = true;
    25. receipt->reserved[0] = 0;
    26. receipt->reserved[1] = 0;
    27. receipt->reserved[2] = 0;
    28.  
    29.  
    30. char * p = (char *) receipt;
    31. for (i=0; i < sizeof(receipt_data_type); i++) {
    32. printf("%02X\r\n", *p++);
    33. }
    34. printf("\r\nAll members of receipt (total size:%d ) \r\n", sizeof(receipt_data_type) );
    35. printf("response_id: %02X (size: %d )\r\n",receipt->response_id, sizeof(receipt->response_id) );
    36. printf("status_change_id: %02X (size: %d )\r\n",receipt->status_change_id, sizeof(receipt->status_change_id));
    37. printf("result_code: %02X (size: %d )\r\n",receipt->result_code, sizeof(receipt->result_code));
    38. printf("reserved[0]: %02X\r\n",receipt->reserved[0]);
    39. printf("reserved[1]: %02X\r\n",receipt->reserved[1]);
    40. printf("reserved[2]: %02X\r\n",receipt->reserved[2]);
    41.  
    42. delete receipt;
    43.  
    44. return 0;
    45. }
    To copy to clipboard, switch view to plain text mode 

    Expected results:
    sizeof(receipt_data_type) == 10

    Actual results:
    sizeof(receipt_data_type) == 12

    and what's the actual ouput:
    12
    08
    FFFFFFCD
    09
    these two bold bytes shouldn't be here!
    01
    00
    00
    00
    01
    00
    00
    00

    All members of receipt (total size:12 )
    response_id: 812 (size: 2 )
    status_change_id: 01 (size: 4 )
    result_code: 01 (size: 1 )
    reserved[0]: 00
    reserved[1]: 00
    reserved[2]: 00


    Now the weird thing is that if I take out the response_id from the structure it reports the correct size (8).

    What in the name of God am I missing?!

    Thanks in advance,
    Pedro Doria Meunier.
    Last edited by pdoria; 14th October 2009 at 21:10. Reason: SOLVED

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: quint16 weirdness

    you are missing: alignment.
    the compiler aligns the 4 byte integer to an adress divisible by 4. (efficiency reasons mainly; though there are (or have been) cpus that can only access an integer if it is located at such an adresss)

  3. The following user says thank you to caduel for this useful post:

    pdoria (14th October 2009)

  4. #3
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: quint16 weirdness

    Caduel... MANY MANY THANKS!

    I was going bananas with this one!

    In the end, per your advice, I solved it with:

    Qt Code:
    1. #pragma pack(push)
    2. #pragma pack(1)
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] quint16 weirdness

    As I see you are trying to send your struct as a packet trough network. I would recommend way that needs more lines of code (and more work) but I guess it is safer. Because you forgot about endianess. Qt is cross platform so it can run on many platforms and on some of them endianess could be different. I would add method like toByteArray() to your struct which will return QByteArray containing you package so you can send it through the network.
    Unfortunately in this method you should change every struct member to set of bytes and put them in some order into QByteArray. Then on the other side you can have fromByteArray(QByteArray) method to convert back from QByteArray into your struct which will require making for example one integer from 4 bytes read from byte array.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. The following user says thank you to faldzip for this useful post:

    pdoria (15th October 2009)

  7. #5
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: [SOLVED] quint16 weirdness

    Thank you faldzip for your insight.

    Really helpful!

    Best regards,
    Pedro Doria Meunier.

Similar Threads

  1. quint16 compiling problem
    By MarkoSan in forum Qt Programming
    Replies: 5
    Last Post: 30th November 2007, 18:08
  2. Typedef quint16 on ARM and Win32
    By sgrasic in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 16th May 2006, 22:27
  3. How to convert from QString to quint16 ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 31st March 2006, 09:00
  4. quint16 problem !!!
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2006, 22:35
  5. Alpha channel weirdness with QGLContext
    By renaissanz in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2006, 16:10

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.