serialization of structure into a raw memory block

A

Alfonso Morra

I have the following data types:

typedef union {
long l ;
double f;
char* s ;
void* p ;
} Value ;

typedef struct {
int id ;
char *label
Value value ;
size ;
}StructA ;


Does anyone know how I can serialize StructA into a raw memory block and
calculate the number of bytes of the memory block?

I am quite suprised that no one seems to know how to do this, and I have
been given all sorts of incorrect answers and suggestions in the
past. Someone out there should surely know how to do this - if even they
don't know how to do it, but can point me to a reference (my Google
searches have not come up with anything remotely useful) - I'd be most
grateful
 
A

Alfonso Morra

correction:

typedef struct {
int id ;
char *label
Value value ;
size_t size ;
}StructA ;
 
E

Emmanuel Delahaye

Alfonso Morra wrote on 01/10/05 :
I have the following data types:

typedef union {
long l ;
double f;
char* s ;
void* p ;
} Value ;

typedef struct {
int id ;
char *label
Value value ;
size ;
}StructA ;

Does anyone know how I can serialize StructA into a raw memory block and
calculate the number of bytes of the memory block?

sizeof (StructA)


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

Alfonso Morra wrote on 01/10/05 :
I have the following data types:

typedef union {
long l ;
double f;
char* s ;
void* p ;
} Value ;

typedef struct {
int id ;
char *label
Value value ;
size ;
}StructA ;

Does anyone know how I can serialize StructA into a raw memory block and
calculate the number of bytes of the memory block?

sizeof (StructA).

Of course, it doesn't include the pointed blocks (if there are strins,
strlen() is your friend).

What to you exactly mean by 'serialize' ? What is your goal ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
M

Malcolm

Alfonso Morra said:
Does anyone know how I can serialize StructA into a raw memory block and
calculate the number of bytes of the memory block?
C has no built in support for serialisation.

integer types can easily be turned into a stream of bytes - you havbe to
choose big-endian or little endian yourself.
floating point types are in practise almost always IEEE, but the standard
doesn't require this. You can use the ldexp() and related functions in the
amths library to reconstruct a floating point type, or you can use an ASCII
representation.
chars can be ASCII, unless you work with non latin strings.

pointers are difficult. Usually you have to serialise the item pointed to
separately, or store the structure somehow (eg a binary tree can be stored
right child / left child), occasionally you need an elaborate system of
registering pointers and rebuilding them.
 
M

Michael Wojcik

I have the following data types:

typedef union {
long l ;
double f;
char* s ;
void* p ;
} Value ;

typedef struct {
int id ;
char *label
Value value ;
size ;
}StructA ;

Does anyone know how I can serialize StructA into a raw memory block and
calculate the number of bytes of the memory block?

Probably not.

First, try to post actual code that compiles. You're missing a
semicolon after "label" and a type for "size". I see you corrected
the latter in a followup, but not the former; and while it's easy
for us to guess how to fix these typos, it'd be much easier for
everyone if you'd just cut and paste a reasonable-size snippet of
actual source.

Second, the C standard does not define "serialization". Certainly
it's possible in C to do various things that could reasonably be
called "serialization", but we have no way of knowing what your
requirements are. The data in an object of type StructA would be
a very different size if it were serialized as a SOAP object, say,
than if it were serialized using a compact binary form.

Third, the serialization function MUST have a way of knowing what
type of data is stored in Value. When an object of union type is
assigned to, that "fixes" the type of the object, and accessing it
as another type produces Undefined Behavior. It might work on
your implementation, but it's not covered by the standard, and here
we discuss standard C.

--
Michael Wojcik (e-mail address removed)

Some there are, brave, high-souled fellows, who could borrow the world to
play at ball, and never feel the responsibility, whereas others are uneasy
and not themselves with a single shilling that does not belong to them.
-- Arthur Ransome
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top