Closed GoogleCodeExporter closed 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
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
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
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
runs for me with the change... so, not a bug.
Original comment by lahike...@gmail.com
on 10 Sep 2010 at 3:22
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
Original issue reported on code.google.com by
Qing.Lia...@gmail.com
on 10 Sep 2010 at 6:25