473,795 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong with this code ? (struct serialization to raw byte stream)

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*)mall oc(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(&siz e, 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)*size of(char) ;
buff = (unsigned char*)malloc(le n) ;

/*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(c har)) ;

*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(l en+1) ;
memcpy(s,(unsig ned char*)block + sizeof(int) + sizeof(double)+
sizeof(size_t), len) ;

p = (MyStruct*)mall oc(sizeof(*p)) ;

p->l = l ;
p->d = d ;
p->s = s ;

/* free resource */
free(block) ;
block = NULL ;
return p ;
}
Nov 15 '05 #1
3 2115
In article <dh**********@n wrdmz02.dmz.ncs .ea.ibs-infra.bt.com> Alfonso Morra <sw***********@ the-ring.com> writes:
....
length = strlen(m->s) ;
len = sizeof(int) + sizeof(double) + sizeof(size_t) +
(length+1)*size of(char) ;
buff = (unsigned char*)malloc(le n) ;

/*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)) ;
Hrm, the second sizeof(int) is wrong.
/*copy string*/
memmove(buff + sizeof(int) + sizeof(double), m->s,
(strlen(m->s)+1)*sizeof(c har)) ;


And here you need a sizeof(size_t);
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #2
On Mon, 3 Oct 2005 12:07:43 +0000 (UTC), Alfonso Morra
<sw***********@ the-ring.com> 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 - 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*)mall oc(sizeof(*in)) , *out = NULL;
Don't cast the return from malloc. It only allows the compiler to
ignore certain errors which can lead to undefined behavior
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(&siz e, in) ;
pack returns a void*. You don't need the cast to assign it to
memblock.
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)*siz eof(char) ;
sizeof(char) is always 1.
buff = (unsigned char*)malloc(le n) ;

/*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(c har)) ;

*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) ) ;
len is an int. But you are copying sizeof(size_t) bytes. size_t need
not have the same size as an int. Even if it does, size_t is unsigned
so the bit pattern may not be interpreted correctly when treated as an
int.
/* get string*/
s = (char*)malloc(l en+1) ;
memcpy(s,(unsig ned char*)block + sizeof(int) + sizeof(double)+
sizeof(size_t) ,len) ;
What happened to the +1? The "string" in s is not a string because it
is not '/0' terminated.

p = (MyStruct*)mall oc(sizeof(*p)) ;

p->l = l ;
p->d = d ;
p->s = s ;

/* free resource */
free(block) ;
block = NULL ;
return p ;
}

<<Remove the del for email>>
Nov 15 '05 #3
Alfonso Morra wrote:
Hi,
Hey dude -)

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.

I spent several days alone with myself in the bathroom.
Here is my code (it dosen't work - compiles fine, pack appears to work,
but unpack retrieves jibberish and causes program to crash).

And here will my be 2 cent reply.
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"


Don't we mean

#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*)mall oc(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? */

The voices in my head are telling my strdup() isn't part of the
standard. Like also, strdup() returns a pointer to char. I think I
worded this right. About this point and time, I think you got something
more funky going on that what a bored 14 yr old has going on with the
pimped out AOL account.
memblock = (unsigned char*)pack(&siz e, in) ;
out = unpack(memblock ) ;

Is memblock part of the holy standard? I just tried typing $man
memblock and all I got was a blank response.
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) ;

}


Okay, this is getting too technical.


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)*size of(char) ;
buff = (unsigned char*)malloc(le n) ;

/*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(c har)) ;

*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(l en+1) ;
memcpy(s,(unsig ned char*)block + sizeof(int) + sizeof(double)+
sizeof(size_t), len) ;

p = (MyStruct*)mall oc(sizeof(*p)) ;

p->l = l ;
p->d = d ;
p->s = s ;

/* free resource */
free(block) ;
block = NULL ;
return p ;
}

In the end, I couldn't compile this because my OS (FreeBSD 4.8) refused
to recongnize the code. I forgot where this was going.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
3008
by: Alfonso Morra | last post by:
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).
11
2044
by: Alfonso Morra | last post by:
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
5
4496
by: Blmn | last post by:
Hello how can i creat e structure and read the struct from a binary file? binaryreader doesn't accept objects so i have to read like reader.getbyte(), reader.getint32(),... . but it should be a way to create a class líke FileHeader and read compelet header at once, like C++ . do you know how?
3
2216
by: Dean L. Howen | last post by:
Hi friends, In C++, we can declare a struct and write it into file just by giving the address the strct instance (using &). for example: ///////////////////////////////// typedef struct Test { int intV; char charV;
6
3309
by: DBC User | last post by:
Could someone help me understand 'Serializable'? What is this 'Serializable' why do we need it and how do we use it? Any link or simple example would be great.
5
1956
by: dvestal | last post by:
I have a class that needs to be serialized to a byte buffer. I'm using a large, unwieldy, homegrown serialization method, which has to change every time the class's members change, and I think there's probably a better way. I could do the job generically, and in just a few lines, by declaring the class as , then using a BinaryFormatter to serialize the object to a memory stream, then getting the buffer from the memory stream. However,...
16
1787
by: chutsu | last post by:
Ok Here is a problem, I got a imaginary database program that I need to code, to add a patient I have function inser_patient. but when I try to input the details it doesn't quite work the way I wanted it to. Code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h>
5
1853
by: Smokey Grindel | last post by:
Anyone able to figure whats wrong here? It is saying it cant convert from itself to itself? I am confused on this error that the designer is throwing... One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes. Object of type 'Comp.Bene.Security.Company' cannot be converted to type 'Comp.Bene.Security.Company'.
2
2203
by: CindyH | last post by:
Hi Trying to get this code to work for http xml post. I need the post to be xml (doc.outerxml) sent in single key name as stream. The following is the post code and code for receiving the request. When it hits the "myresponse = myrequest.GetResponse()" in the post code - I'm getting following error: "The remote server returned an error: (500) Internal Server Error". I don't know if I have something wrong with the post or receiving it on...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.