D
Debajyoti Sarma
How to do serialization in C ? Question arises from serialization of
binary tree.
binary tree.
Very hard to do well. Really serialization is so fundamental it shouldHow to do serialization in C ? Question arises from serialization of
binary tree.
Malcolm said:Very hard to do well. Really serialization is so fundamental it should
be built into the language.
A tree can be serialised as a set of nested parentheses, if the
requirement is to save as text. Look up Newick tree format.
However there's no easy was to write a generalised serialise function
for a general tree containing arbitrary data.
How to do serialization in C ? Question arises from serialization of
binary tree.
Vincenzo Mercuri said:Debajyoti Sarma ha scritto:
a quick search on google gave me 3 main results:
http://tpl.sourceforge.net/
but I can't say how they work or if they fully
support storage of nested structures.
It means a data structure has to be stored as an ordered sequence ofSo fundamental that I don't think I've ever had to do it. I'm not even sure
what it means (flattening?).
Depends on what you mean by "nested". TPL does not handle any data
organisation that uses pointers so linked lists and tree have to be done
by hand. In effect, TPL will do the leaves, but that's about it.
ok.
Can't we store number of structure variables in file?
I thing that will do .... by putting 2 separate tree traversal in the
file....we can reconstruct the tree.
However there's no easy was to write a generalised serialise function
for a general tree containing arbitrary data.
static struct base_ops base_ops
{
int (*print_nicely)(FILE *stream, const struct base *base);
};
How to do serialization in C ? Question arises from serialization of
binary tree.
How to do serialization in C ? Question arises from serialization of
binary tree.
Sorry. Of course type tags have the same issues as vtbl pointers. IsTo get a general purpose facility, it's probably simplest to include a
small integer type tag in each data structure. The tags index a table
of vtbl pointers, and the vtbls have the custom serializer and
deserializer for each type. The various schemes you see to embed vtbl
pointers in the structs themselves at a common location all seem to
have portability issues (not?).
How to do serialization in C ? Question arises from serialization of
binary tree.
Sorry. Of course type tags have the same issues as vtbl pointers. Is
there a portable way to reference common content in different struct
types, or is a gi-normous union (with its inherent dead memory and
compilation issues) the only way?
Sorry. Of course type tags have the same issues as vtbl pointers. Is
there a portable way to reference common content in different struct
types, or is a gi-normous union (with its inherent dead memory and
compilation issues) the only way?
C99 6.7.2.1 Structure and union specifiers, p13
----v----
[...] A pointer to a structure object, suitably converted, points to its
initial member (or if that member is a bit-field, then to the unit in
which it resides), and vice versa. [...]
----^----
About the same is written under C89 6.5.2.1.
lacos- Hide quoted text -
- Show quoted text -
nonsense.tag to be sure we're getting the tag of a point, but that's
typedef struct base_s {
int tag;
} BASE:
typedef struct point_s {
int tag;
double x, y, z,
} POINT;
typedef struct name_s {
int tag;
char name[10];
} NAME;
We would like to be able to pass round BASE* p; and say
if (p->tag == POINT_TAG) ... handle a point ...
else if (p->tag == NAME) ... handle a name.
typedef struct base_s {
int tag;
} BASE:typedef struct point_s {
int tag;
double x, y, z,
} POINT;typedef struct name_s {
int tag;
char name[10];
} NAME;We would like to be able to pass round BASE* p; and sayif (p->tag == POINT_TAG) ... handle a point ...
else if (p->tag == NAME) ... handle a name.
typedef struct base_s {
int tag;
} BASE;
typedef struct point_s {
BASE b;
double x, y, z,
} POINT;
typedef struct name_s {
BASE b;
char name[10];
} NAME;
if (POINT_TAG == p->tag) handle_point((POINT *)p);
Or, in your original example, forget BASE completely, and pass around
pointers-to-int ((int*)).
if (POINT_TAG == *p) handle_point((POINT *)p);
lacos- Hide quoted text -
- Show quoted text -
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.