/ * get int * / memmove(& l,block,sizeof(int)); / * get double * / memmove(& d,(unsigned char *)block + sizeof(int),sizeof(double)); / * get string length * / memmove(& len,(unsigned char *)block + sizeof(int)+ sizeof(double), sizeof(size_t)); / * get string * / s =(char *)malloc(len + 1); memmove(s,(unsigned char *)block + sizeof(int)+ sizeof(double )+ sizeof(size_t),len); p =(MyStruct *)malloc(sizeof(* p)); p-> l = l; p-> d = d; p-> s = s; / *免费资源* / 免费(块); block = NULL; 返回p; } Hi, I am at the end of my tether now - after spending several days trying tofigure how to do this. I have finally written a simple "proof ofconcept" program to test serializing a structure containing pointersinto a "flattened" bit stream. Here is my code (it dosen''t work - compiles fine, pack appears to work,but unpack retrieves jibberish and causes program to crash). I would be grateful for any feedback that helps fix this. My intentionis to build on this example, and use the ideas here, to be able topersist any data structure (I''ll write different pack/unpack routinesfor different data stuctures, just to keep things simple). Anyway,here''s the code:#include "stdlib.h"#include "stdio.h"#include "string.h"typedef struct {intl ;doubled ;char*s; /* Null terminated string */} MyStruct ;void * pack(size_t *size, MyStruct* m);MyStruct *unpack(void* block);int main(int argc, char* argv[]) { MyStruct *in = (MyStruct*)malloc(sizeof(*in)), *out = NULL;unsigned char *memblock = NULL ;size_t size ; in->l = 1000 ;in->d = 3.142857;in->s = strdup("Simple Text" ); memblock = (unsigned char*)pack(&size, in) ;out = unpack(memblock) ; printf("Int member has value : %d (expected : %d)", out->l, in->l ) ;printf("Double member has value : %f (expected : %f)", out->d, in->d ) ;printf("Int member has value : %s (expected : %s)", out->s, in->s ) ; free(in->s) ;free(in) ;free(out->s) ;free(out) ; } void * pack(size_t *size, MyStruct* m) {unsigned char *buff = NULL ;size_t len, length ; length = strlen(m->s) ;len = sizeof(int) + sizeof(double) + sizeof(size_t) +(length+1)*sizeof(char) ;buff = (unsigned char*)malloc(len) ; /*copy int*/memmove(buff, &(m->l), sizeof(int)) ;/*copy double*/memmove(buff + sizeof(int), &(m->d), sizeof(double)) ;/*store length of string*/memmove(buff + sizeof(int) + sizeof(int), &length, sizeof(size_t));/*copy string*/memmove(buff + sizeof(int) + sizeof(double), m->s,(length+1)*sizeof(char)) ; *size = len ;return buff ;}MyStruct *unpack(void* block) {int l, len ;double d ;char * s = NULL ;MyStruct *p = NULL ; /* get int*/memmove(&l, block, sizeof(int)) ;/* get double*/memmove(&d, (unsigned char*)block + sizeof(int), sizeof(double)) ;/* get string length*/memmove(&len, (unsigned char*)block + sizeof(int) + sizeof(double),sizeof(size_t)) ;/* get string*/s = (char*)malloc(len+1) ;memmove(s,(unsigned char*)block + sizeof(int) + sizeof(double)+sizeof(size_t),len) ; p = (MyStruct*)malloc(sizeof(*p)) ; p->l = l ;p->d = d ;p->s = s ; /* free resource */free(block) ;block = NULL ;return p ;} 推荐答案 " Alfonso Morra" < SW *********** @ the-ring.com>在消息中写道 news:dh ********** @ nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com ... "Alfonso Morra" <sw***********@the-ring.com> wrote in messagenews:dh**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com... 我现在处于系绳的尽头 - 花了好几天试图想知道如何做到这一点。我终于写了一个简单的概念证明。测试将包含指针的结构序列化为扁平化的程序。比特流。 这是我的代码(它不起作用 - 编译好,打包似乎工作,但解压缩检索乱码并导致程序崩溃)。 我很感激任何有助于解决此问题的反馈。我的意图是建立在这个例子的基础上,并使用这里的想法,以便能够坚持任何数据结构(我会为不同的数据结构编写不同的打包/解包例程 ,只是为了让事情变得简单)。无论如何,这里是代码: #include" stdlib.h" #include" stdio.h" #include" string .h" typedef struct { int l; double d; char * s; / * Null终止字符串* / } MyStruct; void * pack(size_t * size,MyStruct * m); MyStruct * unpack(void * block); int main(int argc,char * argv []){ MyStruct * in =(MyStruct *)malloc(sizeof(* in)),* out = NULL; unsigned char * memblock = NULL; size_t size; in-> l = 1000; in-> d = 3.142857; in - > s = strdup(" Simple Text"); memblock =(unsigned char *)pack(& size,in); out = unpack(memblock); printf(Int成员有价值:%d(预期:%d),out-> l,in-> l); printf(" Double member)具有以下值:%f(预期:%f)",out-> d,in-> d); printf(" Int member has value:%s(expected:%s)" ,out-> s,in-> s); free(in-> s); free(in); free(out-> s); 免费(外出); } void * pack(size_t * size,MyStruct * m){ unsigned char * buff = NULL; size_t len,length; length = strlen(m-> s); len = sizeof(int)+ sizeof(double)+ sizeof(size_t)+ (长度+ 1)* sizeof(char); buff =(unsigned char *)malloc(len); / * copy int * / memmove(buff, &(m-> l),sizeof(int)); / * copy double * / memmove(buff + sizeof(int),&(m-> d),sizeof (double)); / *存储字符串长度* / memmove(buff + sizeof(int)+ sizeof(int),& length,sizeof(size_t)); memmove(buff + sizeof(int)+ sizeof(double),& length,sizeof(size_t)); / * copy string * / memmove(buff + sizeof(int)+ sizeof(double),m-> s,(length + 1)* sizeof(char)); memmove(buff + sizeof(int)+ sizeof(double)+ sizeof(size_t),m-> s, (长度+ 1) * sizeof(char)); * size = len; 返回buff; } MyStruct * unpack(void * block){ int l,len; double d; char * s = NULL; MyStruct * p = NULL; / * get int * / memmove(& l,block,sizeof(int)); / * get double * / memmove(& d,(unsigned char *)block + sizeof(int),sizeof (double)); / *获取字符串长度* / memmove(& len,(unsigned char *)block + sizeof(int)+ sizeof(double), sizeof(size_t );; / * get string * / s =(char *)malloc(len + 1); memmove(s,(unsigned char *)block + sizeof (int)+ sizeof(double)+ sizeof(size_t),len); memmove(s,(unsigned char *)block + sizeof(int)+ sizeof(double)+ sizeof(size_t),len + 1) ; p =(MyStruct *)malloc(sizeof(* p)); p-> l = l; p-> d = d ; p-> s = s; / *免费资源* / 免费(块); 块= NULL; 返回p ; } Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure containing pointers into a "flattened" bit stream. Here is my code (it dosen''t work - compiles fine, pack appears to work, but unpack retrieves jibberish and causes program to crash). I would be grateful for any feedback that helps fix this. My intention is to build on this example, and use the ideas here, to be able to persist any data structure (I''ll write different pack/unpack routines for different data stuctures, just to keep things simple). Anyway, here''s the code: #include "stdlib.h" #include "stdio.h" #include "string.h" typedef struct { int l ; double d ; char* s; /* Null terminated string */ } MyStruct ; void * pack(size_t *size, MyStruct* m); MyStruct *unpack(void* block); int main(int argc, char* argv[]) { MyStruct *in = (MyStruct*)malloc(sizeof(*in)), *out = NULL; unsigned char *memblock = NULL ; size_t size ; in->l = 1000 ; in->d = 3.142857; in->s = strdup("Simple Text" ); memblock = (unsigned char*)pack(&size, in) ; out = unpack(memblock) ; printf("Int member has value : %d (expected : %d)", out->l, in->l ) ; printf("Double member has value : %f (expected : %f)", out->d, in->d ) ; printf("Int member has value : %s (expected : %s)", out->s, in->s ) ; free(in->s) ; free(in) ; free(out->s) ; free(out) ; } void * pack(size_t *size, MyStruct* m) { unsigned char *buff = NULL ; size_t len, length ; length = strlen(m->s) ; len = sizeof(int) + sizeof(double) + sizeof(size_t) + (length+1)*sizeof(char) ; buff = (unsigned char*)malloc(len) ; /*copy int*/ memmove(buff, &(m->l), sizeof(int)) ; /*copy double*/ memmove(buff + sizeof(int), &(m->d), sizeof(double)) ; /*store length of string*/ memmove(buff + sizeof(int) + sizeof(int), &length, sizeof(size_t));memmove(buff + sizeof(int) + sizeof(double), &length, sizeof(size_t)); /*copy string*/ memmove(buff + sizeof(int) + sizeof(double), m->s, (length+1)*sizeof(char)) ;memmove(buff + sizeof(int) + sizeof(double) + sizeof(size_t), m->s,(length+1)*sizeof(char)) ; *size = len ; return buff ; } MyStruct *unpack(void* block) { int l, len ; double d ; char * s = NULL ; MyStruct *p = NULL ; /* get int*/ memmove(&l, block, sizeof(int)) ; /* get double*/ memmove(&d, (unsigned char*)block + sizeof(int), sizeof(double)) ; /* get string length*/ memmove(&len, (unsigned char*)block + sizeof(int) + sizeof(double), sizeof(size_t)) ; /* get string*/ s = (char*)malloc(len+1) ; memmove(s,(unsigned char*)block + sizeof(int) + sizeof(double)+ sizeof(size_t),len) ;memmove(s,(unsigned char*)block + sizeof(int) + sizeof(double)+sizeof(size_t),len+1) ; p = (MyStruct*)malloc(sizeof(*p)) ; p->l = l ; p->d = d ; p->s = s ; /* free resource */ free(block) ; block = NULL ; return p ; } BTW,我没有看到任何理由更喜欢memmove()到memcpy()。 - David Hilsee BTW, I don''t see any reason to prefer memmove() to memcpy(). --David Hilsee Alfonso Morra写道:Alfonso Morra wrote: 概念证明。测试将包含指针的结构序列化为扁平化的程序。比特流。 这是我的代码(它不起作用 - 编译好,打包似乎工作,但解压缩检索乱码并导致程序崩溃)。 我很感激任何有助于解决此问题的反馈。我的意图是建立在这个例子的基础上,并使用这里的想法,以便能够坚持任何数据结构(我会为不同的数据结构编写不同的打包/解包例程 ,只是为了让事情变得简单)。无论如何,这里是代码: #include" stdlib.h" #include" stdio.h" #include" string .h" typedef struct { int l; double d; char * s; / * Null终止字符串* / } MyStruct; void * pack(size_t * size,MyStruct * m); MyStruct * unpack(void * block); int main(int argc,char * argv []){ MyStruct * in =(MyStruct *)malloc(sizeof(* in)),* out = NULL; unsigned char * memblock = NULL; size_t size; in-> l = 1000; in-> d = 3.142857; in - > s = strdup(" Simple Text"); memblock =(unsigned char *)pack(& size,in); out = unpack(memblock); printf(Int成员有价值:%d(预期:%d),out-> l,in-> l); printf(" Double member)具有以下值:%f(预期:%f)",out-> d,in-> d); printf(" Int member has value:%s(expected:%s)" ,out-> s,in-> s); free(in-> s); free(in); free(out-> s); 免费(外出); } void * pack(size_t * size,MyStruct * m){ unsigned char * bu ff = NULL; size_t len,length; length = strlen(m-> s); len = sizeof(int)+ sizeof(double)+ sizeof( size_t)+ (长度+ 1)* sizeof(char); buff =(unsigned char *)malloc(len); / * copy int * / memmove(buff,&(m-> l),sizeof(int)); / * copy double * / memmove(buff + sizeof(int),&(m- > d),sizeof(double)); / *存储字符串长度* / memmove(buff + sizeof(int)+ sizeof(int),& length,sizeof(size_t)) ; / *复制字符串* / memmove(buff + sizeof(int)+ sizeof(double),m-> s,(长度+ 1)* sizeof(字符)) ; * size = len; 返回buff; } MyStruct * unpack(void * block){ int l, len; double d; char * s = NULL; MyStruct * p = NULL; / * get int * / memmove(& ; l,block,sizeof(int)); / * get double * / memmove(& d,(unsigned char *)block + sizeof(int),sizeof(double)); / * get string le ngm * / memmove(& len,(unsigned char *)block + sizeof(int)+ sizeof(double), sizeof(size_t)); / * get string * / s =(char *)malloc(len + 1); memmove(s,(unsigned char *)block + sizeof(int)+ sizeof(double)+ sizeof(size_t) ,len); p =(MyStruct *)malloc(sizeof(* p)); p-> l = l; p-> ; d = d; p-> s = s; / *免费资源* / 免费(块); 块= NULL; return p; } Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure containing pointers into a "flattened" bit stream. Here is my code (it dosen''t work - compiles fine, pack appears to work, but unpack retrieves jibberish and causes program to crash). I would be grateful for any feedback that helps fix this. My intention is to build on this example, and use the ideas here, to be able to persist any data structure (I''ll write different pack/unpack routines for different data stuctures, just to keep things simple). Anyway, here''s the code: #include "stdlib.h" #include "stdio.h" #include "string.h" typedef struct { intl ; doubled ; char*s; /* Null terminated string */ } MyStruct ; void * pack(size_t *size, MyStruct* m); MyStruct *unpack(void* block); int main(int argc, char* argv[]) { MyStruct *in = (MyStruct*)malloc(sizeof(*in)), *out = NULL; unsigned char *memblock = NULL ; size_t size ; in->l = 1000 ; in->d = 3.142857; in->s = strdup("Simple Text" ); memblock = (unsigned char*)pack(&size, in) ; out = unpack(memblock) ; printf("Int member has value : %d (expected : %d)", out->l, in->l ) ; printf("Double member has value : %f (expected : %f)", out->d, in->d ) ; printf("Int member has value : %s (expected : %s)", out->s, in->s ) ; free(in->s) ; free(in) ; free(out->s) ; free(out) ; } void * pack(size_t *size, MyStruct* m) { unsigned char *buff = NULL ; size_t len, length ; length = strlen(m->s) ; len = sizeof(int) + sizeof(double) + sizeof(size_t) + (length+1)*sizeof(char) ; buff = (unsigned char*)malloc(len) ; /*copy int*/ memmove(buff, &(m->l), sizeof(int)) ; /*copy double*/ memmove(buff + sizeof(int), &(m->d), sizeof(double)) ; /*store length of string*/ memmove(buff + sizeof(int) + sizeof(int), &length, sizeof(size_t)); /*copy string*/ memmove(buff + sizeof(int) + sizeof(double), m->s, (length+1)*sizeof(char)) ; *size = len ; return buff ; } MyStruct *unpack(void* block) { int l, len ; double d ; char * s = NULL ; MyStruct *p = NULL ; /* get int*/ memmove(&l, block, sizeof(int)) ; /* get double*/ memmove(&d, (unsigned char*)block + sizeof(int), sizeof(double)) ; /* get string length*/ memmove(&len, (unsigned char*)block + sizeof(int) + sizeof(double), sizeof(size_t)) ; /* get string*/ s = (char*)malloc(len+1) ; memmove(s,(unsigned char*)block + sizeof(int) + sizeof(double)+ sizeof(size_t),len) ; p = (MyStruct*)malloc(sizeof(*p)) ; p->l = l ; p->d = d ; p->s = s ; /* free resource */ free(block) ; block = NULL ; return p ; } 结构的成员不一定包含在最小的 空间中他们。通常编译器会将 较大尺寸的元素放在4或8或甚至16字节的偏移量上,并将b $ b字节插入pad中。必要时结构的字段之间的空格。使用 " sizeof"计算场偏移是不可靠的,或者甚至可能是b $ b b是正确的。存在偏移。宏将通过从字段的地址中减去结构的地址 来报告结构中字段的实际 偏移量。但是,解压缩数据的例程必须使用与打包数据的程序相同的对齐设置进行编译,这不是一个安全的假设。 。 或者,您只需使用BER(ASN.1)库对数据进行编码。并且 然后总是有XML来以非常便携的格式对数据进行编码。 Greg The members of a struct are not necessarily packed into the smallestspace that will hold them. Usually the compiler will place thelarger-sized elements at 4 or 8 or even 16 byte offsets and insertbytes to "pad" the space between fields of a struct if necessary. Using"sizeof" to calculate the field offsets is not reliable or likely evento be correct. There is an "offset" macro that will report the actualoffset of a field in a struct by substracting the address of the structfrom the address of the field. But then the routine to unpack the datamust be compiled with the same alignment settings as the program thatpacked the data, and that is not a safe assumption to make either. Alternately, you just use a BER (ASN.1) library to encode the data. Andthen there is always XML to encode the data in a very portable format. Greg David Hilsee写道: David Hilsee wrote: " Alfonso Morra" < SW *********** @ the-ring.com>在消息中写道新闻:dh ********** @ nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com ... "Alfonso Morra" <sw***********@the-ring.com> wrote in message news:dh**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com... 我现在处于系绳的尽头 - 花了好几天试图想知道如何做到这一点。我终于写了一个简单的概念证明。测试将包含指针的结构序列化为扁平化的程序。比特流。 这是我的代码(它不起作用 - 编译好,打包似乎工作,但解压缩检索乱码并导致程序崩溃)。 我很感激任何有助于解决此问题的反馈。我的意图是建立在这个例子的基础上,并使用这里的想法,以便能够坚持任何数据结构(我会为不同的数据结构编写不同的打包/解包例程 ,只是为了让事情变得简单)。无论如何,这里是代码: #include" stdlib.h" #include" stdio.h" #include" string .h" typedef struct { int l; double d; char * s; / * Null终止字符串* / } MyStruct; void * pack(size_t * size,MyStruct * m); MyStruct * unpack(void * block); int main(int argc,char * argv []){ MyStruct * in =(MyStruct *)malloc(sizeof(* in)),* out = NULL; unsigned char * memblock = NULL; size_t size; in-> l = 1000; in-> d = 3.142857; in - > s = strdup(" Simple Text"); memblock =(unsigned char *)pack(& size,in); out = unpack(memblock); printf(Int成员有价值:%d(预期:%d),out-> l,in-> l); printf(" Double member)具有以下值:%f(预期:%f)",out-> d,in-> d); printf(" Int member has value:%s(expected:%s)" ,out-> s,in-> s); free(in-> s); free(in); free(out-> s); 免费(外出); } void * pack(size_t * size,MyStruct * m){ unsigned char * buff = NULL; size_t len,length; length = strlen(m-> s); len = sizeof(int)+ sizeof(double)+ sizeof(size_t)+ (长度+ 1)* sizeof(char); buff =(unsigned char *)malloc(len); / * copy int * / memmove(buff,&(m-> l),sizeof(int)); / * copy double * / memmove(buff + sizeof(int),&(m-> d),sizeof(double)); / *存储字符串长度* / memmove(buff + sizeof(int)+ sizeof(int),& length,sizeof(size_t)); Hi,I am at the end of my tether now - after spending several days trying tofigure how to do this. I have finally written a simple "proof ofconcept" program to test serializing a structure containing pointersinto a "flattened" bit stream.Here is my code (it dosen''t work - compiles fine, pack appears to work,but unpack retrieves jibberish and causes program to crash).I would be grateful for any feedback that helps fix this. My intentionis to build on this example, and use the ideas here, to be able topersist any data structure (I''ll write different pack/unpack routinesfor different data stuctures, just to keep things simple). Anyway,here''s the code:#include "stdlib.h"#include "stdio.h"#include "string.h"typedef struct {int l ;double d ;char* s; /* Null terminated string */} MyStruct ;void * pack(size_t *size, MyStruct* m);MyStruct *unpack(void* block);int main(int argc, char* argv[]) {MyStruct *in = (MyStruct*)malloc(sizeof(*in)), *out = NULL;unsigned char *memblock = NULL ;size_t size ;in->l = 1000 ;in->d = 3.142857;in->s = strdup("Simple Text" );memblock = (unsigned char*)pack(&size, in) ;out = unpack(memblock) ;printf("Int member has value : %d (expected : %d)", out->l, in->l ) ;printf("Double member has value : %f (expected : %f)", out->d, in->d ) ;printf("Int member has value : %s (expected : %s)", out->s, in->s ) ;free(in->s) ;free(in) ;free(out->s) ;free(out) ;} void * pack(size_t *size, MyStruct* m) {unsigned char *buff = NULL ;size_t len, length ;length = strlen(m->s) ;len = sizeof(int) + sizeof(double) + sizeof(size_t) +(length+1)*sizeof(char) ;buff = (unsigned char*)malloc(len) ;/*copy int*/memmove(buff, &(m->l), sizeof(int)) ;/*copy double*/memmove(buff + sizeof(int), &(m->d), sizeof(double)) ;/*store length of string*/memmove(buff + sizeof(int) + sizeof(int), &length, sizeof(size_t)); memmove(buff + sizeof(int)+ sizeof(double),& length,sizeof(size_t)); memmove(buff + sizeof(int) + sizeof(double), &length, sizeof(size_t)); / * copy string * / memmove(buff + sizeof(int)+ sizeof(double),m-> s,(length + 1)* sizeof(char)); /*copy string*/memmove(buff + sizeof(int) + sizeof(double), m->s,(length+1)*sizeof(char)) ; memmove( buff + sizeof(int)+ sizeof(double)+ sizeof(size_t),m-> s,(length + 1)* sizeof(char)); memmove(buff + sizeof(int) + sizeof(double) + sizeof(size_t), m->s, (length+1)*sizeof(char)) ; * size = len; 返回buff; } MyStruct * unpack(void * block){ int l,len; double d; char * s = N. ULL; MyStruct * p = NULL; / * get int * / memmove(& l,block,sizeof(int)); / * get double * / memmove(& d,(unsigned char *)block + sizeof(int),sizeof(double)); / * get string length * / memmove(& ; len,(unsigned char *)block + sizeof(int)+ sizeof(double), sizeof(size_t)); / * get string * / s =(char *) malloc(len + 1); memmove(s,(unsigned char *)block + sizeof(int)+ sizeof(double)+ sizeof(size_t),len); *size = len ;return buff ;}MyStruct *unpack(void* block) {int l, len ;double d ;char * s = NULL ;MyStruct *p = NULL ;/* get int*/memmove(&l, block, sizeof(int)) ;/* get double*/memmove(&d, (unsigned char*)block + sizeof(int), sizeof(double)) ;/* get string length*/memmove(&len, (unsigned char*)block + sizeof(int) + sizeof(double),sizeof(size_t)) ;/* get string*/s = (char*)malloc(len+1) ;memmove(s,(unsigned char*)block + sizeof(int) + sizeof(double)+sizeof(size_t),len) ; memmove(s,(unsigned char *)block + sizeof(int)+ sizeof(double)+ sizeof(size_t),len + 1); memmove(s,(unsigned char*)block + sizeof(int) + sizeof(double)+ sizeof(size_t),len+1) ; p =(MyStruct *)malloc(sizeof(* p)); p-> l = l; p-> d = d; p-> s = s; / *免费资源* / 免费(块); 块= NULL; return p; } p = (MyStruct*)malloc(sizeof(*p)) ;p->l = l ;p->d = d ;p->s = s ;/* free resource */free(block) ;block = NULL ;return p ;} BTW,我没有看到任何理由更喜欢memmove()到memcpy()。 BTW, I don''t see any reason to prefer memmove() to memcpy(). 很多人,非常感谢大卫 - 你的帮助很多ppreciated! Many, many thanks David - your help is much appreciated ! 这篇关于这段代码出了什么问题? (struct serialization to raw memoryblock)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 13:53