如何在Google Protobuff中编码和解码向量

发布于 2025-01-29 21:14:12 字数 838 浏览 2 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一生独一 2025-02-05 21:14:12
for(int i1=0; i1<proto->tailist_size(); i1++)
{
   mempy(tailist.tai.plmn, proto->tailist(i1).tai().plmn().value(), 3);
}

您正在尝试解码向量。那个向量在哪里?您在哪里创建要写入的裁缝?您不会在矢量中添加裁缝,而是在每次迭代中覆盖它。

这应该是这样的:

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);
}
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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文