Is it possible to move data from a QByteArray
-
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!
-
Hi,
Why not directly pass the data from the QByteArray using "this":http://qt-project.org/doc/qt-4.8/qbytearray.html#data ?
-
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?
-
Just to be sure: what options does this library offer to parse your data ? And how do they work memory handling wise ?
-
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.
-
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.
-
It would really be useful to know what the library authors mean by "take ownership"
-
As in you give them a pointer and they will delete it once it's done, without copying.
-
Then indeed, you can't give them the QByteArray data to delete