Chain of Pointer in a file?

D

Davy

Hi all,

I have a struct. The struct contain several chains(pointer something
like nodes->next).
How to save this struct to a file and how to read it back to ram?

I am confused of the matter that the pointer is the absolute address of
the ram. When the file was re-loaded into the ram. All the address will
be obsolete and should be discarded.

Am I right?

Best regards,
Davy
 
R

Richard Bos

Davy said:
I have a struct. The struct contain several chains(pointer something
like nodes->next).
How to save this struct to a file and how to read it back to ram?

Find another way to represent the link structure; the pointers will, as
you suspect, be obsolete by the time you read the file back in.
For example, number all structs; then, for each struct, write its
number, then its data, and for each pointer it contains, write the
number of the struct it points at.

Richard
 
K

Keith Thompson

Davy said:
I have a struct. The struct contain several chains(pointer something
like nodes->next).
How to save this struct to a file and how to read it back to ram?

I am confused of the matter that the pointer is the absolute address of
the ram. When the file was re-loaded into the ram. All the address will
be obsolete and should be discarded.

I don't know of any straightforward way to do what you're trying to
do. Addresses stored in a file are valid only during the execution of
the program (which means there's seldom much point in storing them in
a file).

If the file is an array of nodes, you can replace node pointers with
indices into the file. This requires consistenly mapping the
addresses to indices, and storing a different node type in the file
than you use in memory (unless you use a union of a pointer and an
integer index).

Or you can redesign the data structure you use in the program. For
example, you can use an array of nodes (expanded with realloc() as
necessary), and use indices into the array rather than pointers. The
array can then be written to a file and read back later, and the
indices will still be valid.
 
J

Jean-Claude Arbaut

Le 16/06/2005 10:16, dans
(e-mail address removed), « Davy »
Hi all,

I have a struct. The struct contain several chains(pointer something
like nodes->next).
How to save this struct to a file and how to read it back to ram?

I am confused of the matter that the pointer is the absolute address of
the ram. When the file was re-loaded into the ram. All the address will
be obsolete and should be discarded.

Am I right?

Best regards,
Davy

Depending on the structure, you can simply write your nodes
in the file in some special order: for example, write a
linked list in head -> tail order (or the reverse), you
don't loose any vital information. If nodes have varying size,
you may need to add node sizes to your file, before each node.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top