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. Qt analog to C struct?
Forum Update on Monday, May 27th 2025

Qt analog to C struct?

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 4 Posters 7.7k Views
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi -

    My app uses a message header of a fixed size. I'd intended to do this:

        struct
        {
            QByteArray hmac = QByteArray(32, '\0');
            QByteArray iv = QByteArray(16, '\0');
            QByteArray username = QByteArray(100, '\0');
            QByteArray password = QByteArray(100, '\0');
            QByteArray nonce = QByteArray(16, '\0');
            uint8_t timestamp[23];
            uint8_t reserved[1];
        } msgHeader;
    
    

    But I seem to get pointers to the QByteArrays in the structure, so this might not be the right approach.

    Is there a Qt data structure that allows me to do this, or should I just keep all the fields as uint8_t, and convert to/from QByteArrays for processing?

    kshegunovK 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi -

      My app uses a message header of a fixed size. I'd intended to do this:

          struct
          {
              QByteArray hmac = QByteArray(32, '\0');
              QByteArray iv = QByteArray(16, '\0');
              QByteArray username = QByteArray(100, '\0');
              QByteArray password = QByteArray(100, '\0');
              QByteArray nonce = QByteArray(16, '\0');
              uint8_t timestamp[23];
              uint8_t reserved[1];
          } msgHeader;
      
      

      But I seem to get pointers to the QByteArrays in the structure, so this might not be the right approach.

      Is there a Qt data structure that allows me to do this, or should I just keep all the fields as uint8_t, and convert to/from QByteArrays for processing?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @mzimmers said in Qt analog to C struct?:

      But I seem to get pointers to the QByteArrays in the structure, so this might not be the right approach.

      Rightfully so. QByteArray uses implicit sharing and is the size of void * the actual data is kept in the private object.

      Is there a Qt data structure that allows me to do this

      No.

      or should I just keep all the fields as uint8_t

      Yes, or use qint8 (they're aliases)

      convert to/from QByteArrays for processing

      Attach a QByteArray to the data without copying it. Use QByteArray::fromRawData for this and then pass it on for processing.

      PS:
      @Wieland also suggested, rightfully so, to use std::array instead in this case.

      Read and abide by the Qt Code of Conduct

      mzimmersM 1 Reply Last reply
      2
      • kshegunovK kshegunov

        @mzimmers said in Qt analog to C struct?:

        But I seem to get pointers to the QByteArrays in the structure, so this might not be the right approach.

        Rightfully so. QByteArray uses implicit sharing and is the size of void * the actual data is kept in the private object.

        Is there a Qt data structure that allows me to do this

        No.

        or should I just keep all the fields as uint8_t

        Yes, or use qint8 (they're aliases)

        convert to/from QByteArrays for processing

        Attach a QByteArray to the data without copying it. Use QByteArray::fromRawData for this and then pass it on for processing.

        PS:
        @Wieland also suggested, rightfully so, to use std::array instead in this case.

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        I'm not sure I understand...what should I use std::array for exactly?

        kshegunovK ? 2 Replies Last reply
        0
        • mzimmersM mzimmers

          I'm not sure I understand...what should I use std::array for exactly?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          std::array is a thin wrapper (template) around a statically sized array of a given type. So you could use it instead of int[], for example:

          struct
          {
              // ...
              std::array<uint8_t, 23> timestamp;
              std::array<uint8_t, 1> reserved;
          } msgHeader;
          

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • mzimmersM mzimmers

            I'm not sure I understand...what should I use std::array for exactly?

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #5

            As a replacement for the QByteArrays and the C-style array:

            struct MsgHeader
            {
                std::array<uint8_t, 32> hmac = {};
                // ...
                std::array<uint8_t, 23> timestamp = {};
            };
            
            mzimmersM 1 Reply Last reply
            2
            • ? A Former User

              As a replacement for the QByteArrays and the C-style array:

              struct MsgHeader
              {
                  std::array<uint8_t, 32> hmac = {};
                  // ...
                  std::array<uint8_t, 23> timestamp = {};
              };
              
              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #6

              @Wieland OK. What is the advantage of this -- automatic range checking or some other safeties?

              kshegunovK 1 Reply Last reply
              0
              • mzimmersM mzimmers

                @Wieland OK. What is the advantage of this -- automatic range checking or some other safeties?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                yes, also somewhat more type-safe as it does not decay readily to void *, is a C++ object ... and, well, the cool kids are using it (not that I approve, but that's whole other rant) ... ;)

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                2
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Yes, automatic range checking, increased safety against unintentional pointer mistakes, and compared to QByteArray, increased performance.

                  JonBJ 1 Reply Last reply
                  2
                  • mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #9

                    OK, thank you both.

                    1 Reply Last reply
                    1
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      Lot of cross-posting going on here ^_^

                      kshegunovK 1 Reply Last reply
                      1
                      • ? A Former User

                        Lot of cross-posting going on here ^_^

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by
                        #11

                        Twice in less than 10 minutes, must be a forum record or something.

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        0
                        • mzimmersM Offline
                          mzimmersM Offline
                          mzimmers
                          wrote on last edited by
                          #12

                          Just to pursue this a little further, if I do use the std::array template, can I still do a memcpy, or do I have to assign the elements individually? I couldn't see anything in the cPPreference page on this.

                          Also, would it be better just to create one large QByteArray and use pointers to access delimited areas within it? (I'm still a little surprised that Qt doesn't have some construct to handle this).

                          ? kshegunovK 3 Replies Last reply
                          0
                          • mzimmersM mzimmers

                            Just to pursue this a little further, if I do use the std::array template, can I still do a memcpy, or do I have to assign the elements individually? I couldn't see anything in the cPPreference page on this.

                            Also, would it be better just to create one large QByteArray and use pointers to access delimited areas within it? (I'm still a little surprised that Qt doesn't have some construct to handle this).

                            ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #13

                            @mzimmers said in Qt analog to C struct?:

                            can I still do a memcpy

                            The correct way to copy the array is to use std::copy. It's up to the implementation of your standard library to then find the fastest way to copy your data.

                            1 Reply Last reply
                            0
                            • mzimmersM mzimmers

                              Just to pursue this a little further, if I do use the std::array template, can I still do a memcpy, or do I have to assign the elements individually? I couldn't see anything in the cPPreference page on this.

                              Also, would it be better just to create one large QByteArray and use pointers to access delimited areas within it? (I'm still a little surprised that Qt doesn't have some construct to handle this).

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by kshegunov
                              #14

                              @mzimmers said in Qt analog to C struct?:

                              if I do use the std::array template, can I still do a memcpy

                              Yes, std::array is an aggregate (means it has only one member that is your regular type[] array). Although(!), you should copy it like any other object - it is an object after all.

                              Also, would it be better just to create one large QByteArray and use pointers to access delimited areas within it?

                              And throw away type safety? Why? There's no significant difference between that and having a struct containing fixed sized arrays.

                              I'm still a little surprised that Qt doesn't have some construct to handle this

                              To handle what exactly?

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              0
                              • mzimmersM mzimmers

                                Just to pursue this a little further, if I do use the std::array template, can I still do a memcpy, or do I have to assign the elements individually? I couldn't see anything in the cPPreference page on this.

                                Also, would it be better just to create one large QByteArray and use pointers to access delimited areas within it? (I'm still a little surprised that Qt doesn't have some construct to handle this).

                                ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #15

                                @mzimmers said in Qt analog to C struct?:

                                Also, would it be better just to create one large QByteArray and use pointers to access delimited areas within it?

                                I don't see why you would do that. Just complicates your code for no reason.

                                1 Reply Last reply
                                1
                                • mzimmersM Offline
                                  mzimmersM Offline
                                  mzimmers
                                  wrote on last edited by
                                  #16

                                  I'm just looking for a clean way to handle a data structure. In all candor, both the memcpy_s and the std::array/std::copy approaches are tough on the eyes. I guess, though, that's the price you pay to avoid overrunning boundaries.

                                  kshegunovK 1 Reply Last reply
                                  0
                                  • mzimmersM mzimmers

                                    I'm just looking for a clean way to handle a data structure. In all candor, both the memcpy_s and the std::array/std::copy approaches are tough on the eyes. I guess, though, that's the price you pay to avoid overrunning boundaries.

                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on last edited by kshegunov
                                    #17

                                    I don't follow, perhaps I'm missing something. Suppose you use std::array. It'd look something like this:

                                    struct X
                                    {
                                        std::array<int, 4> a;
                                        std::array<double, 2> b;
                                    };
                                    

                                    You use it as any POD struct.

                                    X x = {
                                        { 1, 2, 3, 4 }, // This is an initializer list for std::array<int, 4>
                                        { 0.1, 12.3 }   // The second member's intialization
                                    };
                                    
                                    X y = x; // This is all, copying of the arrays is taken care for you by the STL and the compiler
                                    
                                    double z[2];
                                    std::memcpy(z, x.b.data(), x.b.size()); // and voila, we copied the data to `z`
                                    

                                    Read and abide by the Qt Code of Conduct

                                    1 Reply Last reply
                                    0
                                    • mzimmersM Offline
                                      mzimmersM Offline
                                      mzimmers
                                      wrote on last edited by
                                      #18

                                      I'm probably being too fussy, but given how easily one can assign/copy QByteArray and QString objects, it's a shame IMO that there's no way to aggregate them as in a C struct. But it's probably their very flexibility that makes such use unfeasible.

                                      It's OK...I'm content to use memcpy_s, but it's just not attractive code.

                                      kshegunovK 1 Reply Last reply
                                      0
                                      • ? A Former User

                                        Yes, automatic range checking, increased safety against unintentional pointer mistakes, and compared to QByteArray, increased performance.

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by JonB
                                        #19

                                        @Wieland said in Qt analog to C struct?:

                                        Yes, automatic range checking,

                                        You said this in response to why std::array should be used/preferred. What "automatic range checking" are you saying it provides, under what circumstances?

                                        1 Reply Last reply
                                        0
                                        • mzimmersM mzimmers

                                          I'm probably being too fussy, but given how easily one can assign/copy QByteArray and QString objects, it's a shame IMO that there's no way to aggregate them as in a C struct. But it's probably their very flexibility that makes such use unfeasible.

                                          It's OK...I'm content to use memcpy_s, but it's just not attractive code.

                                          kshegunovK Offline
                                          kshegunovK Offline
                                          kshegunov
                                          Moderators
                                          wrote on last edited by kshegunov
                                          #20

                                          @mzimmers said in Qt analog to C struct?:

                                          I'm probably being too fussy,

                                          Not really, but it would help to know what you dislike about the above example and why you think you need to use memcpy?

                                          @JNBarchan said in Qt analog to C struct?:

                                          What "automatic range checking" are you saying it provides, under what circumstances?

                                          std::array<int, 3> x;
                                          int z = x[3]; //< Regular arrays allow this (generally)
                                          x[3] = 0;     //< Regular arrays mostly allow this too
                                          

                                          e.g. run this through your debugger:

                                          int main(int argc, char ** argv)
                                          {
                                              int z[2];
                                              z[2] = 0;
                                          }
                                          

                                          Read and abide by the Qt Code of Conduct

                                          JonBJ mzimmersM 2 Replies Last reply
                                          0

                                          • Login

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