October 21, 2010

phamtv phamtv
Lab Rat
135 posts

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

Page  
1

Can someone provide me and example on how to get this to work?

17 replies

October 21, 2010

phamtv phamtv
Lab Rat
135 posts

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!

October 21, 2010

phamtv phamtv
Lab Rat
135 posts

Sorry this post is under the wrong thread…

October 21, 2010

kkrzewniak kkrzewniak
Lab Rat
219 posts

Have You tried QDataStream with a QBuffer.

  1. struct YourStruct
  2. {
  3.     quint8 x;
  4.     quint16 y;
  5.     float z;
  6.  
  7.     YourStruct()
  8.         :x(0),y(0),z(0.0f)
  9.     {}
  10.  
  11.     YourStruct(const YourStruct& other)
  12.         :x(other.x),y(other.y),z(other.z)
  13.     {}
  14.  
  15.     bool operator == (const WeaponStats& other) const
  16.     {
  17.         ...
  18.     }
  19.  
  20.     bool operator < (const WeaponStats& other) const
  21.     {
  22.        ...
  23.     }
  24.  
  25.     bool operator > (const WeaponStats& other) const
  26.     {
  27.        ...
  28.     }
  29. };
  30.  
  31. inline QDataStream& operator<<(QDataStream& out, const YourStruct& str)
  32. {
  33.     out << str.x;
  34.     out << str.y;
  35.     out << str.z;
  36.     return out;
  37. }
  38.  
  39. inline QDataStream& operator>>(QDataStream& in, YourStruct& str)
  40. {
  41.     in >> str.x;
  42.     in >> str.y;
  43.     in >> str.z;
  44.     return in;
  45. }
  46.  
  47. Q_DECLARE_METATYPE(YourStruct)
  48.  
  49. //before using call
  50. qRegisterMetaTypeStreamOperators<YourStructs>("YourStruct");

or just have a look here [qtcentre.org]

 Signature 

Me, Grimlock, not “nice dino”. ME BASH BRAINS!

October 21, 2010

kkrzewniak kkrzewniak
Lab Rat
219 posts

Post Irrelevant since Merged

 Signature 

Me, Grimlock, not “nice dino”. ME BASH BRAINS!

October 21, 2010

phamtv phamtv
Lab Rat
135 posts

is there not a way I can use memmove?

October 21, 2010

kkrzewniak kkrzewniak
Lab Rat
219 posts

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

 Signature 

Me, Grimlock, not “nice dino”. ME BASH BRAINS!

October 21, 2010

phamtv phamtv
Lab Rat
135 posts

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.

  1.     struct_MtrTxMsgType TX_Msg;
  2.     int txcnt, rcvLen;
  3.     QByteArray TX_Buffer;
  4.     TX_Buffer.resize(2049);
  5.     QByteArray RX_Buffer;
  6.     RX_Buffer.resize(2049);
  7.     bool ok;
  8.  
  9.     memset(&TX_Msg.WakeUp[0], 0xFE, sizeof(TX_Msg.WakeUp));
  10.     TX_Msg.Header = 0x68;
  11.     TX_Msg.Address[0] = 0x09;
  12.     TX_Msg.Address[1] = 0x90;
  13.     TX_Msg.Address[2] = 0x78;
  14.     TX_Msg.Address[3] = 0x56;
  15.     TX_Msg.Address[4] = 0x34;
  16.     TX_Msg.Address[5] = 0x12;
  17.     TX_Msg.Delimeter = 0x68;
  18.     TX_Msg.R_W = 0x01;
  19.     TX_Msg.DataLength = 0x02;
  20.  
  21.     memmove((void *)TX_Buffer.data(), &TX_Msg, sizeof(TX_Msg));

October 21, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

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

October 21, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3383 posts

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

October 21, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3383 posts

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.

October 21, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

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.

October 21, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3383 posts

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.

October 21, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3383 posts

phamtv: You did read the documentation of QByteArray [doc.trolltech.com], didn’t you? It states quite clearly how to access its buffer.

October 21, 2010

phamtv phamtv
Lab Rat
135 posts

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???

October 21, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3383 posts

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.

Page  
1

  ‹‹ Is it possible to set a background image to a widget?      How can I do fast animation without dropping frames? ››

You must log in to post a reply. Not a member yet? Register here!