Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. why Struct size does not match the data inside?
Qt 6.11 is out! See what's new in the release blog

why Struct size does not match the data inside?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 675 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    myjtag
    wrote on last edited by
    #1

    Hi,
    I have a hardware that would accept Some commands, and the commands should have a specific size,

    struct SamplingCard{
            short cmd;
            uint32_t CapDelay;
            uint32_t CapCnt;
        };
    
        union  {
            SamplingCard Card;
            char  array[1460];
        } command;
    
    QByteArray wBa;
    
    wBa = QByteArray::fromRawData(command.array, sizeof(SamplingCard));
    

    The problem is that I need the QbyteArray to be exactly 1460 bytes, But it is 12, also the size of struct should be 10Bytes, But Qt would consider it 12 bytes! any work around this?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      This has nothing to do with Qt.
      The struct is padding to respect alignment, use #pragma pack if you want to get rid of it:

      #include <QDebug>
      #include <QByteArray>
      #pragma pack(2)
      struct SamplingCard{
          qint16 cmd;
          quint32 CapDelay;
          quint32 CapCnt;
      };
      
      int main()
      {
          SamplingCard command{1,2,3};
          QByteArray wBa(1460,0);
          std::memcpy(wBa.data(),&command,sizeof(SamplingCard));
          qDebug().noquote() << wBa.toHex();
          return 0;
      }

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      7
      • M Offline
        M Offline
        myjtag
        wrote on last edited by
        #3

        @VRonin said in why Struct size does not match the data inside?:

        #pragma pack

        Thanks for the tip.

        1 Reply Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          Keep in mind that #pragma controlls implementation defined behavior, and isn't subject to official standardization. If the development environment doesn't match the deployment environment, don't forget to check both.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved