这段代码出了什么问题? (struct serialization to raw byte str [英] What's wrong with this code ? (struct serialization to raw byte str

查看:51
本文介绍了这段代码出了什么问题? (struct serialization to raw byte str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我现在正处于系绳的尽头 - 花了几天时间试图

计算如何做到这一点。我终于写了一个简单的证明

概念用于测试将包含指针

的结构序列化为扁平化的程序。比特流。


这是我的代码(它不起作用)。


如果有任何有助于修复的反馈,我将不胜感激这个。我的意图

是建立在这个例子上,并使用这里的想法,以便能够坚持任何数据结构(我会为不同的程序编写不同的例程) br />
结构,只是为了简单起见)。无论如何,这里是代码:

#include" stdlib.h"

#include" stdio.h"

#包括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 has value:%f(expected:%f),out-> d,in-> d);

printf(" Int member has value:%s (预期:%s)",out-> s,in-> s);


免费(in-> s);

免费(里);

免费(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(字符);

buff =(unsigned char *)malloc(len);


/ * copy int * /

memmove(buff,&(m-> l),sizeof(int));

/ * copy双倍* /

memmo ve(buff + sizeof(int),&(m-> d),sizeof(double));

/ *字符串存储长度* /

memmove (buff + sizeof(int)+ sizeof(int),& length,sizeof(size_t));

/ * copy string * /

memmove(buff + sizeof (int)+ sizeof(double),m-> s,

(strlen(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 * /

memcpy(& l,block,sizeof(int));

/ * get double * /

memcpy(& d,(unsigned char *)block + sizeof(int),sizeof(double));

/ * get string length * /

memcpy(& len,(unsigned char *)block + sizeof(int)+ sizeof(double),

sizeof(size_t));

/ * get string * /

s =(char *)malloc(len + 1);

memcpy(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 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).

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 routines for difdferent
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" ); /*did I need to strdup? */

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,
(strlen(m->s)+1)*sizeof(char)) ;

*size = len ;
return buff ;
}
MyStruct *unpack(void* block) {
int l, len ;
double d ;
char * s = NULL ;
MyStruct *p = NULL ;

/* get int*/
memcpy(&l, block, sizeof(int)) ;
/* get double*/
memcpy(&d, (unsigned char*)block + sizeof(int), sizeof(double)) ;
/* get string length*/
memcpy(&len, (unsigned char*)block + sizeof(int) + sizeof(double),
sizeof(size_t)) ;
/* get string*/
s = (char*)malloc(len+1) ;
memcpy(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写道:
Alfonso Morra wrote:


我现在处于系绳的尽头 - 花了好几天试图想知道如何做到这一点。我终于写了一个简单的
概念证明。测试将包含指针的结构序列化为扁平化的程序。比特流。

这是我的代码(它不起作用)。
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).




究竟是怎么回事?编译错误?分段故障?等等..



Exactly how doesn''t it work? Compile error? Segmentation Fault? Etc..





David G. Hong写道:


David G. Hong wrote:
Alfonso Morra写道:
Alfonso Morra wrote:


我现在处于系绳的尽头 - 花了几天时间试图了解如何去做这个。我终于写了一个简单的
概念证明。测试将包含指针的结构序列化为扁平化的程序。比特流。

这是我的代码(它不起作用)。
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).



究竟是怎么回事?编译错误?分段故障?等等..


Exactly how doesn''t it work? Compile error? Segmentation Fault? Etc..




< snip>

(它不能正常工作 - 编译好,包似乎工作,但解压

检索乱码并导致程序崩溃)。

< / snip>



<snip>
(it dosen''t work - compiles fine, pack appears to work, but unpack
retrieves jibberish and causes program to crash).
</snip>


只需一个错误(或者更确切地说是两行):沮丧的错误,我相信,你现在肯定找到了!但是既然你要求

评论,那么它们就是:


Alfonso Morra写道:
Just one error (or rather on two lines): an error of frustration, I
believe, one you surely have found by now! But since you asked for
comments, here they are:

Alfonso Morra wrote:
MyStruct * in =(MyStruct *)malloc(sizeof(* in)),* out = NULL;


这没关系,但演员有什么特别的理由吗? (我也尝试用

来写sizeof *因为sizeof是一个运算符,当应用于

对象时,但这是风格的,因人而异。)

in-> s = strdup(" Simple Text"); / *我需要调整吗? * /


取决于你的意思:你可以通过mallocing strlen + 1

然后做strcpy来复制文本。或者,您可以直接指定

简单文本。只要你记得不同的实例,就可以了解字符串文字的
不需要引用单独的内存块,而这个内存块可能不是
可修改的。

memblock =(unsigned char *)pack(& size,in);


包装返回void *的任何原因正在转换为未签名

char *?


len = sizeof(int)+ sizeof(double)+ sizeof(size_t)+
(length + 1)* sizeof(char);


您选择不使用更透明尺寸的原因m-> l +

sizeof m-> d + sizeof length +(长度+ 1)* sizeof(char)?


不使用sizeof类型,只使用sizeof对象经常避免

错误,如下所示

memmove(buff + sizeof(int),&(m-> d),sizeof(double));


您可能知道这一点,但在没有括号的情况下,后缀 - > d

已经在前缀&前评估了。

/ *存储字符串长度* /
memmove(buff + sizeof(int)+ sizeof(int),& length,sizeof(size_t));
^^^^^^^^^^^

嗯? (下一行也有类似的错误)。你使用memmove

的原因是什么? malloced内存永远不会与另一个区域重叠。


为了避免这种错误,我尝试维护一个正在运行的指针curpos

,它随每个memcpy和形成目标的基础

的进一步memcpys。


顺便说一下,如果你得到了它,为什么要存储字符串长度
$ b首先是$ b strlen,如果你要存储最终的空字节

呢?没错,我只是困惑。

/ *免费资源* /
免费(阻止);
阻止= NULL;
返回p;
MyStruct *in = (MyStruct*)malloc(sizeof(*in)), *out = NULL;
It does not matter, but any particular reason for the cast? (I also try
to write sizeof *in since sizeof is an operator when applied to
objects, but that is stylistic and varies from person to person).
in->s = strdup("Simple Text" ); /*did I need to strdup? */
Depends on what you mean: you could copy the text by mallocing strlen+1
and then doing a strcpy. Alternatively, you could directly assign
"Simple Text" to in->s as long as you remember that different instances
of string literals need not refer to separate blocks of memory, and
this block of memory may not be modifiable.
memblock = (unsigned char*)pack(&size, in) ;
Any reason that pack returns void* which is being converted to unsigned
char*?

len = sizeof(int) + sizeof(double) + sizeof(size_t) +
(length+1)*sizeof(char) ;
An reason you chose not to use the more transparent sizeof m->l +
sizeof m->d + sizeof length + (length+1) * sizeof (char)?

Not using sizeof types, and only using sizeof objects often avoids
errors like the following
memmove(buff + sizeof(int), &(m->d), sizeof(double)) ;
You may know this, but in the absence of parentheses, the postfix ->d
is already evaluated before prefix &.
/*store length of string*/
memmove(buff + sizeof(int) + sizeof(int), &length, sizeof(size_t)); ^^^^^^^^^^^
Huh? (similar mistake also on next line). Any reason you used memmove
in all of this? malloced memory can never overlap another area.

To avoid this kind of error, I try to maintain a running pointer curpos
which is incremented with each memcpy and forms the base for the target
of further memcpys.

Incidentally, why did you store the string length if you got it by
strlen in the first place, and if you are storing the final null byte
anyway? Not wrong, I am just confused.
/* free resource */
free(block) ;
block = NULL ;
return p ;




在返回之前分配给块的实现是什么?


顺便说一下,从代码中写入stdout的最后一个应该以

换行,并且没有换行的两个很多字符很难读取




What does assigning to block achieve just before a return?

Incidentally, the last write to stdout from your code should end in a
newline, and two many characters without a newline is rather hard to
read!


这篇关于这段代码出了什么问题? (struct serialization to raw byte str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆