I have an embedded C++ project where I'm reading a series of int32's from a hardware device, then packing them into an int array as part of large data structure, and then sending to a remote system over TCP/IP. So, I was using a simple data struct with a bunch of stuff defined and now I want to convert this to use Protocol Buffers. So, I was thinking of using a "repeated int32 data" as the element of my proto buff. But I want to avoid using a loop such as this:
int hardware_data[1000]; // An array that holds the data read from the hardware
for(int i=0; i< sizeof(hardware_data); i++ )
{
proto.add_data( hardware_data[i] );
}
I'd much rather use an efficient method, such as making the proto buff just point to the existing hardware_data[] array (a zero copy method), or using memcpy from hardware_data into proto.data.
I understand how to setup the memcpy(), but how then does the proto buff know how many elements are in the proto.data "array"? Can I still use the proto.data_size() to get the number of elements? Is there an efficient way to move the data from my hardware read to the proto buff for sending? Is there a better way to do this?
Kerrik, I wasn't aware of the zero copy API. Here's my proto definition:
message hardware_data
{
optional Lob lob = 1;
optional int32 taskSeqNum = 2;
optional int32 secondsOfDay = 3;
optional float IQOutRateKhz = 4;
optional float IQBwKhz = 5;
optional int32 tStart = 6;
optional int32 tOffset = 7;
optional float collectionTime = 8;
optional int32 numSamples = 9;
optional int32 chunk = 10;
optional int32 dimSize = 11;
repeated int32 data = 12 [packed=true];
}
I'm not sure how the zero copy would play into this proto buff definition.
sizeofoperator returns the size of the data structure in bytes, so for 1000-item array ofints it is probably 4000 and your loop will index the array far past its actual end, thus causing undefined behavior. Usesizeof(hadware_data)/sizeof(hardware_data[0])to calculate the number of items of the array. - CiaPan