[zeromq-dev] how to receive a message into a static buffer?
Martin Sustrik
sustrik at fastmq.com
Mon May 4 16:06:28 CEST 2009
Hi Aamir,
> I would like to receive a message into a pre-allocated buffer. I tried
> the following:
>
> void* buffer = &mybuffer;
> zmq::message_t msg( buffer, sizeof(T), NULL );
> api-> receive( &msg, true );
> assert( msg.data() == datap ); // fails here
>
> I have to resort to a memcpy:
>
> zmq::message_t msg();
> api-> receive( &msg, true );
> memcpy( &mybuffer, msg.data(), sizeof(T) );
>
> I want to avoid the copy and I must have the message data in the static
> buffer. Is this possible?
The data are read from the network asynchronously in the background.
Thus the buffer is allocated there. When you do 'receive' the buffer is
passed to your application (in form of pointer). Note that this way 0MQ
is able to transfer the data without copying it.
In case you want to get the data to your own buffer, you have to copy
it. There's no way to avoid the copy as your buffer doesn't even have to
exist at the time when the data are retrieved from the network.
Martin
More information about the zeromq-dev
mailing list