Sending / Receiving of byte arrays

Thiago Macieira thiago at kde.org
Wed Feb 8 13:56:28 PST 2012


On Wednesday, 8 de February de 2012 22.18.36, Bogdan Lotko wrote:
> Hello,
> 
> I have to pass the structure of bit-fields over the D-Bus connection
> 
> e.g.
> 
> struct XXX
> {
>     uint state1 : 2;
>     ...
>     uint stateN : 5:
> }
> 
> I thought (because of possible endianity) the ByteArray will be the best
> transport for it.

If you're trying to transport a bitfield to another process that might have a 
different endianness, I hope you know what you're doing. Please note that the 
*order* of the bitfields change when you change the endianness.

If you have not taken that care, don't try to transport the bitfields. Instead, 
expand them to full bytes at least.

> So I'd like to use the
> 
> char  buff[n];
> 
> and cast it to the struct XXX pointer
> 
> XXX *xxxStructPtr = ((XXX *)(buff));
> xxxStructPtr->state1 = myStateEnum;

This is ugly. And it might be breaking strict aliasing.

Do you really need bitfields? Can't you use regular byte-sized integers?

> After filling the buffer I have to pass it to the QByteArray, I thought
> the fromRawData(buff,n) will be most appropriate for it.

That creates a QByteArray without copying the bytes.

> What I've been wondering, was if I can use the QByteArray directly as a
> "buffer", i.e to cast it to get the pointer to my XXX struct.

You cannot cast a QByteArray. But you can get the pointer to its data.

> Or maybe there is a quite different solution for it?

Yes, you could design less ugly code.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.freedesktop.org/archives/dbus/attachments/20120208/12aa0eb9/attachment.pgp>


More information about the dbus mailing list