如何在Google Protobuff中编码和解码向量
我在main中具有以下结构。cppi
typedef struct s1
{
uint8 plmn[3];
}tai_s;
typedef struct s2
{
tai_s tai;
}tailist_s;
std::vector<tailist_s> tallist;
在main.proto
message tai_s
{
google.protobuf.BytesValue plmn[3];
}
message tailist_s
{
tai_s tai;
}
repeated tailist_s tallist;
im中试图像下面的原始物体一样,
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);
}
我试图像下面的原始案例那样解码原子,
for(int i1=0; i1<proto->tailist_size(); i1++)
{
mempy(tailist.tai.plmn, proto->tailist(i1).tai().plmn().value(), 3);
}
但是在memcpy期间它给出了分割故障。请让我知道我做错了什么。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试解码向量。那个向量在哪里?您在哪里创建要写入的裁缝?您不会在矢量中添加裁缝,而是在每次迭代中覆盖它。
这应该是这样的:
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: