'How to encode and decode vector in google protobuff
I have following structure in main.cpp
typedef struct s1
{
uint8 plmn[3];
}tai_s;
typedef struct s2
{
tai_s tai;
}tailist_s;
std::vector<tailist_s> tallist;
I have folowing structure in main.proto
message tai_s
{
google.protobuf.BytesValue plmn[3];
}
message tailist_s
{
tai_s tai;
}
repeated tailist_s tallist;
Im trying to encode protobuff like below,
for(int i1=0; i1<tailist.size(); i1++)
{
const tailist_s *tailistproto = proto->add_tailist();
tailistproto->mutable_tai()->mutable_plmn()->set_value(tailist.tai.plmn, 3);
}
Im trying to decode protobuff like below,
for(int i1=0; i1<proto->tailist_size(); i1++)
{
mempy(tailist.tai.plmn, proto->tailist(i1).tai().plmn().value(), 3);
}
But it is giving segmentation fault during memcpy. Please let me know what i'm doing wrong.
Solution 1:[1]
for(int i1=0; i1<proto->tailist_size(); i1++)
{
mempy(tailist.tai.plmn, proto->tailist(i1).tai().plmn().value(), 3);
}
You are trying to decode a vector. Where is that vector? Where do you create the tailist you are trying to write to? You aren't adding the tailist to the vector and overwrite it in every iteration.
This should be something like this:
std::vector<tailist_s> tallist(proto->tailist_size());
for(int i1=0; i1<proto->tailist_size(); i1++)
{
mempy(&tallist[i1].tai.plmn, proto->tailist(i1).tai().plmn().value(), 3);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Goswin von Brederlow |
