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 Updated to NodeBB v4.3 + New Features

Qt analog to C struct?

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 4 Posters 7.7k Views 3 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
    mzimmers
    wrote on 2 Oct 2017, 19:13 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?

    K 1 Reply Last reply 2 Oct 2017, 19:32
    0
    • M mzimmers
      2 Oct 2017, 19:13

      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?

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 2 Oct 2017, 19:32 last edited by kshegunov 10 Feb 2017, 19:36
      #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

      M 1 Reply Last reply 2 Oct 2017, 19:41
      2
      • K kshegunov
        2 Oct 2017, 19:32

        @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.

        M Offline
        M Offline
        mzimmers
        wrote on 2 Oct 2017, 19:41 last edited by
        #3

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

        K ? 2 Replies Last reply 2 Oct 2017, 19:52
        0
        • M mzimmers
          2 Oct 2017, 19:41

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

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 2 Oct 2017, 19:52 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
          • M mzimmers
            2 Oct 2017, 19:41

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

            ? Offline
            ? Offline
            A Former User
            wrote on 2 Oct 2017, 19:52 last edited by A Former User 10 Feb 2017, 19:55
            #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 = {};
            };
            
            M 1 Reply Last reply 2 Oct 2017, 19:55
            2
            • ? A Former User
              2 Oct 2017, 19:52

              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 = {};
              };
              
              M Offline
              M Offline
              mzimmers
              wrote on 2 Oct 2017, 19:55 last edited by
              #6

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

              K 1 Reply Last reply 2 Oct 2017, 19:57
              0
              • M mzimmers
                2 Oct 2017, 19:55

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

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 2 Oct 2017, 19:57 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 2 Oct 2017, 19:58 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 3 Oct 2017, 14:13
                  2
                  • M Offline
                    M Offline
                    mzimmers
                    wrote on 2 Oct 2017, 19:58 last edited by
                    #9

                    OK, thank you both.

                    1 Reply Last reply
                    1
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on 2 Oct 2017, 19:59 last edited by
                      #10

                      Lot of cross-posting going on here ^_^

                      K 1 Reply Last reply 2 Oct 2017, 20:00
                      1
                      • ? A Former User
                        2 Oct 2017, 19:59

                        Lot of cross-posting going on here ^_^

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 2 Oct 2017, 20:00 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
                        • M Offline
                          M Offline
                          mzimmers
                          wrote on 2 Oct 2017, 21:42 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).

                          ? K 3 Replies Last reply 2 Oct 2017, 21:50
                          0
                          • M mzimmers
                            2 Oct 2017, 21:42

                            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 2 Oct 2017, 21:50 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
                            • M mzimmers
                              2 Oct 2017, 21:42

                              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).

                              K Offline
                              K Offline
                              kshegunov
                              Moderators
                              wrote on 2 Oct 2017, 21:52 last edited by kshegunov 10 Feb 2017, 21:53
                              #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
                              • M mzimmers
                                2 Oct 2017, 21:42

                                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 2 Oct 2017, 21:52 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
                                • M Offline
                                  M Offline
                                  mzimmers
                                  wrote on 2 Oct 2017, 22:48 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.

                                  K 1 Reply Last reply 2 Oct 2017, 23:00
                                  0
                                  • M mzimmers
                                    2 Oct 2017, 22:48

                                    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.

                                    K Offline
                                    K Offline
                                    kshegunov
                                    Moderators
                                    wrote on 2 Oct 2017, 23:00 last edited by kshegunov 10 Feb 2017, 23:01
                                    #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
                                    • M Offline
                                      M Offline
                                      mzimmers
                                      wrote on 3 Oct 2017, 13:58 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.

                                      K 1 Reply Last reply 3 Oct 2017, 14:35
                                      0
                                      • ? A Former User
                                        2 Oct 2017, 19:58

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

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on 3 Oct 2017, 14:13 last edited by JonB 10 Mar 2017, 14:15
                                        #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
                                        • M mzimmers
                                          3 Oct 2017, 13:58

                                          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.

                                          K Offline
                                          K Offline
                                          kshegunov
                                          Moderators
                                          wrote on 3 Oct 2017, 14:35 last edited by kshegunov 10 Mar 2017, 14:43
                                          #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 M 2 Replies Last reply 3 Oct 2017, 14:41
                                          0

                                          1/29

                                          2 Oct 2017, 19:13

                                          • Login

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