memory dump/re-read?

A

Allin Cottrell

A. Suppose my program mallocs and populates an array of structs
of a fixed size apiece. For example, something like:

struct foo {
int i;
double x[8];
char s[16];
};

We want to save this work and re-read it later. One solution
that works well -- so long as we're sure the re-reading will
occur on the same platform as the saving -- is to do a binary
dump of the array of structs with fwrite(), then read them
back into memory later with fread().

B. Now suppose my program mallocs and populates an array
of structs which themselves contain dynamically allocated
members, for example

struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?

Allin Cottrell
Wake Forest University
 
J

Jack Klein

A. Suppose my program mallocs and populates an array of structs
of a fixed size apiece. For example, something like:

struct foo {
int i;
double x[8];
char s[16];
};

We want to save this work and re-read it later. One solution
that works well -- so long as we're sure the re-reading will
occur on the same platform as the saving -- is to do a binary
dump of the array of structs with fwrite(), then read them
back into memory later with fread().

B. Now suppose my program mallocs and populates an array
of structs which themselves contain dynamically allocated
members, for example

struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?

Allin Cottrell
Wake Forest University

Please use a proper signature delimiter, like mine below. It consists
of three characters "-- ", that is two dashes and a space. Most
newsreaders recognize it and handle it properly automatically when
quoting.

As to your question, you can't portably save pointers to a file and
expect the same memory to be usable again when you read them back. For
the array of doubles, I would suggest writing an int first that
specifies how many doubles are to follow, then the doubles themselves.
You can take a similar approach with the character string, or since
you will know when the string starts, you can just dump the string and
its '\0' terminator.

But you will certainly need to decide on some extra work, to be done
when writing and undone when reading back in.
 
C

CBFalconer

Allin said:
A. Suppose my program mallocs and populates an array of structs
of a fixed size apiece. For example, something like:

struct foo {
int i;
double x[8];
char s[16];
};

We want to save this work and re-read it later. One solution
that works well -- so long as we're sure the re-reading will
occur on the same platform as the saving -- is to do a binary
dump of the array of structs with fwrite(), then read them
back into memory later with fread().

B. Now suppose my program mallocs and populates an array
of structs which themselves contain dynamically allocated
members, for example

struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?

Fundamentally, no. You can't get dynamic pointers from anything
except malloc and friends, and you can't control their values. The
sequences returned may vary widely even on separate runs of the
same program with the same data.

One possible fancy footwork is to convert _everything_ to arrays of
known length, and then substitute indices for pointers.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
A

abdur_rab7

Allin said:
B. Now suppose my program mallocs and populates an array
of structs which themselves contain dynamically allocated
members, for example

struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?

Allin Cottrell
Wake Forest University

You can do it by having one more additional file of meta data

struct baz_meta_data {
size_t size_of_double; /* size of double array */
size_t length_of_string; /* length of string */
};
 
M

Malcolm

Allin Cottrell said:
struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?
No. It is no longer possible to save the array with a single C call.

What you need is a serialisation / deserialisation pair of fucntions

/* can only fail if IO fails, which you detect later */
void savebaz(struct baz *b, FILE *fp)
/* return a code for fail */
int loadbaz(struct baz *b, FILE *fp)

You can still save the non-pointer members in binary. If you wnat to port
the file, or read it, use text. The load function is trickier, since you
will need to allocate memory, and also detect malformed input.
 
A

Allin Cottrell

Malcolm said:
No. It is no longer possible to save the array with a single C call.

What you need is a serialisation / deserialisation pair of fucntions

OK, got it. Thanks to all who responded.
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top