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. Is it possible to move data from a QByteArray

Is it possible to move data from a QByteArray

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 4.5k 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.
  • T Offline
    T Offline
    t3685
    wrote on last edited by
    #1

    Hi,

    I am working on a client-server program over tcp and need to process a lot of raw byte data. Using Qt sockets I send and receive my data as QByteArray's. However to parse data I need to copy the data into another parsing class that is written by someone else. However their API also accept pre-allocated char* arrays and since I don't plan to use my QByteArrays again I thought it might be more efficient to just move the data out of the QByteArray, thereby eliminating one needless copy. Is this possible?

    Thanks in advance!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not directly pass the data from the QByteArray using "this":http://qt-project.org/doc/qt-4.8/qbytearray.html#data ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        t3685
        wrote on last edited by
        #3

        Thanks for your reply.

        I'm using that right now (actually const data()), but the parsing class will then take a copy of the data. I would like to take ownership of the data inside the QByteArray, in which case I can just pass ownership to the parsing class. Is this possible?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Just to be sure: what options does this library offer to parse your data ? And how do they work memory handling wise ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            No, it is not possible to take ownership of the memory block you are given by data()/const_data().

            If the parsing routine takes a copy of the (presumably \0 terminated) memory you pass as its const char * argument then it will do that regardless of whether you "own" that memory or the QByteArray does.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              t3685
              wrote on last edited by
              #6

              The parsing class gives me two options: either take a copy of the data (via a pointer) or pass a pointer and the parsing class will take ownership. But it seems that it is not possible to pass ownership, thanks for your answer.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                It would really be useful to know what the library authors mean by "take ownership"

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  t3685
                  wrote on last edited by
                  #8

                  As in you give them a pointer and they will delete it once it's done, without copying.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Then indeed, you can't give them the QByteArray data to delete

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    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