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. [Merged] copying data from structure to a ByteArray and vise versa
QtWS25 Last Chance

[Merged] copying data from structure to a ByteArray and vise versa

Scheduled Pinned Locked Moved General and Desktop
18 Posts 5 Posters 17.1k 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.
  • P Offline
    P Offline
    phamtv
    wrote on last edited by
    #2

    How can I access ByteArray´s underlying data buffer so I can use memmove to copy data from a structure to a ByteArray data type? Please provide example. Thanks!

    1 Reply Last reply
    0
    • P Offline
      P Offline
      phamtv
      wrote on last edited by
      #3

      Sorry this post is under the wrong thread...

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kkrzewniak
        wrote on last edited by
        #4

        Have You tried QDataStream with a QBuffer.
        @
        struct YourStruct
        {
        quint8 x;
        quint16 y;
        float z;

        YourStruct()
            :x(0),y(0),z(0.0f)
        {}
        
        YourStruct(const YourStruct& other)
            :x(other.x),y(other.y),z(other.z)
        {}
        
        bool operator == (const WeaponStats& other) const
        {
            ...
        }
        
        bool operator < (const WeaponStats& other) const
        {
           ...
        }
        
        bool operator > (const WeaponStats& other) const
        {
           ...
        }
        

        };

        inline QDataStream& operator<<(QDataStream& out, const YourStruct& str)
        {
        out << str.x;
        out << str.y;
        out << str.z;
        return out;
        }

        inline QDataStream& operator>>(QDataStream& in, YourStruct& str)
        {
        in >> str.x;
        in >> str.y;
        in >> str.z;
        return in;
        }

        Q_DECLARE_METATYPE(YourStruct)

        //before using call
        qRegisterMetaTypeStreamOperators<YourStructs>("YourStruct");
        @

        or just have a look "here":http://www.qtcentre.org/wiki/index.php?title=Using_custom_data_types_with_Qt#QDataStream

        Me, Grimlock, not "nice dino". ME BASH BRAINS!

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kkrzewniak
          wrote on last edited by
          #5

          Post Irrelevant since Merged

          Me, Grimlock, not "nice dino". ME BASH BRAINS!

          1 Reply Last reply
          0
          • P Offline
            P Offline
            phamtv
            wrote on last edited by
            #6

            is there not a way I can use memmove?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kkrzewniak
              wrote on last edited by
              #7

              char * QByteArray::data ()
              Returns a pointer to the data stored in the byte array.
              Have You tried this.

              Me, Grimlock, not "nice dino". ME BASH BRAINS!

              1 Reply Last reply
              0
              • P Offline
                P Offline
                phamtv
                wrote on last edited by
                #8

                yes I have... the following code I have does not seem to work. I checked under the memory window after the memmove function but do not see the result I am expecting.

                @
                struct_MtrTxMsgType TX_Msg;
                int txcnt, rcvLen;
                QByteArray TX_Buffer;
                TX_Buffer.resize(2049);
                QByteArray RX_Buffer;
                RX_Buffer.resize(2049);
                bool ok;

                memset(&TX_Msg.WakeUp[0], 0xFE, sizeof(TX_Msg.WakeUp));
                TX_Msg.Header = 0x68;
                TX_Msg.Address[0] = 0x09;
                TX_Msg.Address[1] = 0x90;
                TX_Msg.Address[2] = 0x78;
                TX_Msg.Address[3] = 0x56;
                TX_Msg.Address[4] = 0x34;
                TX_Msg.Address[5] = 0x12;
                TX_Msg.Delimeter = 0x68;
                TX_Msg.R_W = 0x01;
                TX_Msg.DataLength = 0x02;
                
                memmove((void *)TX_Buffer.data(), &TX_Msg, sizeof(TX_Msg));
                

                @

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DenisKormalev
                  wrote on last edited by
                  #9

                  Do you need memmove for speedup or for something else? In first case I think using QByteArray will remove all advantages of using memmove.

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tobias.hunger
                    wrote on last edited by
                    #10

                    Denis: Why? You can reserve a chunk of memory and qMemCopy into it. Why is that slower as using some other method?

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tobias.hunger
                      wrote on last edited by
                      #11

                      phamtv: I'd recommend using qMemCopy/qMemSet from QtCore/QtGlobal. I think those are not documented, so they are not officially supported, but they work well for me.

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        DenisKormalev
                        wrote on last edited by
                        #12

                        Tobias, hm, I mostly saying that from my point of view memmove and other c things should be used now only if you are know what you are doing (e.g. if you need to speed up your memory management). In common cases such low-level work is not needed. And in case of mixing memmove for initializing data and using QByteArray to work with it I think initial speedup from memmove will be neglected by QByteArray abstractions.

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          tobias.hunger
                          wrote on last edited by
                          #13

                          Denis: You are right that you should not need these low level functionality 95% of the time... but it is still very useful for the last 5%:-)

                          I actually do not think that the QByteArray overhead is that big by the way (I did not do any measurements though), it is just a wrapper around a piece of memory after all. Resizing the buffer is of course expensive and needs to be avoided.

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            tobias.hunger
                            wrote on last edited by
                            #14

                            phamtv: You did read the "documentation of QByteArray":http://doc.trolltech.com/4.7/qbytearray.html, didn't you? It states quite clearly how to access its buffer.

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              phamtv
                              wrote on last edited by
                              #15

                              yes I have read the QByteArray document. I am still puzzled on the benefits of a QByteArray. My background with byte arrays have always been something similar to quint8 arr[] in which I can later cast to whatever my data structure is. Besides dynamic resizing of the array size, are there any other advantages to QByteArray over the quint8[]? Also, is it true that if I use the QByteArray, I should use the qMemCopy/qMemSet from QtCore/QtGlobal instead of the memmove, memcpy???

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                tobias.hunger
                                wrote on last edited by
                                #16

                                To quote the documentation:

                                QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings. Using QByteArray is much more convenient than using const char *. Behind the scenes, it always ensures that the data is followed by a '\0' terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.

                                Using QByteArray or not does not matter for qMemCopy or memcpy. The first is just a wrapper around the latter anyway. qMemCopy will be there wherever Qt is available, and it is in a Qt header which is included in almost all Qt code.

                                1 Reply Last reply
                                0
                                • P Offline
                                  P Offline
                                  phamtv
                                  wrote on last edited by
                                  #17

                                  Thank you Tobias. As always, your continual support is greatly appreciated!

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    Mart
                                    wrote on last edited by
                                    #18

                                    That time tests:

                                    @ QByteArray ba1(2048, 0);
                                    QByteArray ba2(2048, 255);
                                    QByteArray ba3(1024, 255);
                                    char *ba1data = ba1.data();
                                    char *ba2data = ba2.data();

                                    QElapsedTimer timer;
                                    timer.start();

                                    for (int i = 0; i < 100000000; i++)
                                    {
                                    //ba2.replace(1000, 1024, ba1.data()+1000, 1024); // For 12324 ms.
                                    //ba2.replace(1000, 1024, ba1.mid(1000, 1024)); // For 33228 ms.
                                    //ba3 = ba1.left(1024); // For 45615 ms. <- ?!
                                    //ba3 = ba1.right(1024); // For 29640 ms.
                                    //ba3 = ba1.mid(1000, 1024); // For 29859 ms.

                                    //memcpy(ba2data+1000, ba1data+1000, 1024);       // For 7722 ms.
                                    qMemCopy(ba2data+1000, ba1data+1000, 1024);     // For 11981 ms.
                                    

                                    }

                                    qDebug() << timer.elapsed();@

                                    1 Reply 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