How to initialize a UInt8MultiArray message
Not too much info on these (or any of the variants) on the wiki: what is the proper way to initialize these? I've tried:
const unsigned int data_sz = 10;
std_msgs::UInt8MultiArray m;
m.layout.dim.push_back(std_msgs::MultiArrayDimension());
m.layout.dim[0].size = data_sz;
m.layout.dim[0].stride = 1;
m.layout.dim[0].label = "bla";
// only needed if you don't want to use push_back
m.data.resize(data_sz);
for an array with 10, 1 byte elements, but I keep getting segfaults at the subscriber callback when accessing the data
member (FIXED: see edit).
I couldn't find any examples anywhere, apart from http://alexsleat.co.uk/tag/multiarray/, but those snippets do not initialize any dimensions.
Is it programmers responsibility to keep the dimension objects in sync with the actual size of the data
member, or is ROS doing some magic behind the scenes at serialization?
edit: as @Lorenz commented: reserve
-> resize
. I've left the bit about the segfaults in the question, even though that was fixed.
reserve might not do what you expect. It just causes the vector to allocate more memory but doesn't change its size, i.e. a call to vector's size method will still return 0. Instead, use the method resize.
yes, just GDBed myself to that conclusion as well. Should've read the docs more carefully. Thanks for your reply. Point about the documentation on the whole
MultiArray
thing being really thin / non existent stands though (imho).