Reputation: 2796
I'm trying to initialize a message in ZeroMQ that I can send multiple times without copying memory every time.
According to the documentation, this can be done by using the zmq_msg_copy
function (or message_t::copy()
in C++):
#include <cstdio>
#include <zmq.hpp>
using namespace zmq;
int main() {
int payload = 5;
message_t* msg = new message_t((void*)&payload, sizeof(payload));
message_t copy = message_t(sizeof(payload));
// msg->copy(©); // this fails but does not produce an error
int status = zmq_msg_copy((zmq_msg_t*)copy.data(), (zmq_msg_t*)msg->data());
printf("%i\n", status);
}
I compile with
gcc -lzmq -lstdc++ file.cpp
and run
./a.out
which produces
-1
What am I doing wrong here?
As for source and destination variables:
zmq_msg_copy
has destination first, source second:
int zmq_msg_copy (zmq_msg_t *dest, zmq_msg_t *src);
message_t::copy
is implemented the following way:
inline void copy (message_t const *msg_)
{
int rc = zmq_msg_copy (&msg, const_cast<zmq_msg_t*>(&(msg_->msg)));
if (rc != 0)
throw error_t ();
}
so @vasek is exactly right, the supplied argument needs to be the source.
Upvotes: 0
Views: 2026
Reputation: 2849
According to documentation for message_t argument to copy
should be source message, not the destination.
This code should work (tested under Slackware 14.2/GCC6.3 and MSVC 2017):
message_t copy;
copy.copy(msg);
Also note you don't have to preallocate message buffer for copied message.
Upvotes: 2