shiyilei / protobuf-c

Automatically exported from code.google.com/p/protobuf-c
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

how to pack repeated field? #48

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
centos 5.5 64, gcc 4.1 , protobuf 2.2, protobuf-c 1.4

the proto file is like this:

message country {
        required string         name = 1;
        repeated province       provinces = 2;  
}

message province {
        required string         name = 1;
        repeated city           cities = 2;
}

message city {
        required int32          id = 1;
        required string         name = 2;
}

this is my code:

       Country *country = malloc(sizeof(Country));
        country__init(country);

        Province* provinces[2];
        provinces[0] = malloc(sizeof(provinces));
        provinces[1] = malloc(sizeof(provinces));
        province__init(provinces[0]);
        province__init(provinces[1]);

        City* city_a[2];
        city_a[0] = malloc(sizeof(City));
        city_a[1] = malloc(sizeof(City));
        city__init(city_a[0]);
        city__init(city_a[1]);

        city_a[0]->id = 1;
        city_a[0]->name = "city1";
        city_a[1]->id = 2;
        city_a[1]->name = "city2";

        City* city_b[2];
        city_b[0] = malloc(sizeof(City));
        city_b[1] = malloc(sizeof(City));
        city__init(city_b[0]);
        city__init(city_b[1]);

        city_b[0]->id = 3;
        city_b[0]->name = "city3";
        city_b[1]->id = 4;
        city_b[1]->name = "city4";

        provinces[0]->cities = city_a;
        provinces[0]->name = "anhui";
        provinces[1]->cities = city_b;
        provinces[1]->name = "guangdong";
        provinces[0]->n_cities = 2;
        provinces[1]->n_cities = 2;

        country->provinces = provinces;
        country->name = "china";
        country->n_provinces = 2;

        printf("%s \n", country->name);

        size_t size = country__get_packed_size(country);
        char* buffer = malloc(sizeof(char) * size);
        size_t packed_size = country__pack(country, buffer);
        FILE *fp;
        if ((fp = fopen("test.dat", "wb+")) == NULL) 
        {   
                printf("cannt open file");
                exit(1);
        }   
        fwrite(buffer, sizeof(char), packed_size, fp);  
        fclose(fp);

        return 0;

i got error: Segmentation fault

Original issue reported on code.google.com by Qing.Lia...@gmail.com on 10 Sep 2010 at 6:25

GoogleCodeExporter commented 8 years ago
i got the same error with this code:

#include <stdlib.h>
#include <stdio.h>
#include "test.pb-c.h"

int main(void)
{
        Country *country = malloc(sizeof(Country));
        country__init(country);

        Province provinces[2];
        province__init(&(provinces[0]));
        province__init(&(provinces[1]));

        City city_a[2];
        city__init(&(city_a[0]));
        city__init(&(city_a[1]));

        city_a[0].id = 1;
        city_a[0].name = "city1";
        city_a[1].id = 2;
        city_a[1].name = "city2";

        City city_b[2];
        city__init(&(city_b[0]));
        city__init(&(city_b[1]));

        city_b[0].id = 3;
        city_b[0].name = "city3";
        city_b[1].id = 4;
        city_b[1].name = "city4";

        provinces[0].cities = &city_a;
        provinces[0].name = "anhui";
        provinces[1].cities = &city_b;
        provinces[1].name = "guangdong";
        provinces[0].n_cities = 2;
        provinces[1].n_cities = 2;

        country->provinces = &provinces;
        country->name = "china";
        country->n_provinces = 2;

        printf("%s \n", country->name);

        size_t size = country__get_packed_size(country);
        char* buffer = malloc(sizeof(char) * size);
        size_t packed_size = country__pack(country, buffer);
        FILE *fp;
        if ((fp = fopen("test.dat", "wb+")) == NULL) 
        {   
                printf("cannt open file");
                exit(1);
        }   
        fwrite(buffer, sizeof(char), packed_size, fp);  
        fclose(fp);

        return 0;
}

Original comment by Qing.Lia...@gmail.com on 10 Sep 2010 at 6:56

GoogleCodeExporter commented 8 years ago
good job for the code, but the document is so pool!

Original comment by Qing.Lia...@gmail.com on 10 Sep 2010 at 6:58

GoogleCodeExporter commented 8 years ago
is this a bug ? or i used it in a wrong way? is anybody here?

Original comment by Qing.Lia...@gmail.com on 10 Sep 2010 at 8:27

GoogleCodeExporter commented 8 years ago
Your original code is almost correct... but there's a bug:

        provinces[0] = malloc(sizeof(provinces));
        provinces[1] = malloc(sizeof(provinces));

should be :

        provinces[0] = malloc(sizeof(Province));
        provinces[1] = malloc(sizeof(Province));

regarding Comment 2, the docs are on the wiki -- concrete improvement 
suggestions are welcome, but the docs seem fine to me :)

Original comment by lahike...@gmail.com on 10 Sep 2010 at 3:12

GoogleCodeExporter commented 8 years ago
runs for me with the change... so, not a bug.

Original comment by lahike...@gmail.com on 10 Sep 2010 at 3:22

GoogleCodeExporter commented 8 years ago
so sorry for my stupid code. thanks very much .  my advice for the document is: 
demo should be as simple as possble, just give the code first , then give the 
explain. just an advice.

Original comment by Qing.Lia...@gmail.com on 11 Sep 2010 at 1:38