Segmentation fault problem

Hi!

It works fine when I try to save an instance of a class to a file using:

class Instance instance;
fstream outputfstream(“file”, ios_base::out | ios_base::binary)
outputfstream.write( (const char*) &instance, sizeof(Instance));

But if I later want to read back the instance using:

fstream inputfstream(“file”, ios_base::in | ios_base::binary)
inputfstream.read((char*) &instance, sizeof(Instance)) );

this fails telling me that there is a memory/segmentation fault.
But if using an array:

char array[sizeof(Instance)])
fstream inputfstream(“file”, ios_base::in | ios_base::binary)
inputfstream.read((char*) &array[0], sizeof(Instance)) );

that line works out just fine…

What to do here… Anyone??

Regards Arve

On Thu, 30 Aug 2001 17:05:02 +0200, Arve Slenes <arve@datarespons.no>
wrote:

Hi!

It works fine when I try to save an instance of a class to a file using:

class Instance instance;
fstream outputfstream(“file”, ios_base::out | ios_base::binary)
outputfstream.write( (const char*) &instance, sizeof(Instance));

But if I later want to read back the instance using:

fstream inputfstream(“file”, ios_base::in | ios_base::binary)
inputfstream.read((char*) &instance, sizeof(Instance)) );

this fails telling me that there is a memory/segmentation fault.

Is it at this very line (.read()) that the program fails? Is
“instance” a global variable, and are .write() and .read() in the same
program?

This method of objects’ passivation and activation may work
but for very simple cases (verified): global objects with no pointers,
saved and restored across the same program. In general, (1) .read()
and .write() are overloaded for every class the instances are stored
of, or (2) a kind of class identifier is stored along with the
instance members, so that the activation process could invoke an
appropriate constructor, or (3) data type information of members is
provided for each class, so that the passivation/activation could be
written once and for all.

ako

But if using an array:

char array[sizeof(Instance)])
fstream inputfstream(“file”, ios_base::in | ios_base::binary)
inputfstream.read((char*) &array[0], sizeof(Instance)) );

that line works out just fine…

What to do here… Anyone??

Regards Arve

Andrzej Kocon wrote:

Is it at this very line (.read()) that the program fails? Is
“instance” a global variable, and are .write() and .read() in the same
program?

Well, i don’t think it is on the exact line, but just after (it seems
like)…
The “instance” is not a global instance. It is created somewhere in the
programe,
and then a pointer to this is passed to a class that uses it to do stuff.
Then this very
same “do stuff”-class saves a buch of instances to be able to read back and
use the same data the next time the program starts up.

This method of objects’ passivation and activation may work
but for very simple cases (verified): global objects with no pointers,
saved and restored across the same program.

What alternative methods are there?? I do have some pointers in my
saved class… Could someone post some C++ code examples to
start off with??


In general, (1) .read()
and .write() are overloaded for every class the instances are stored
of, or (2) a kind of class identifier is stored along with the
instance members, so that the activation process could invoke an
appropriate constructor, or (3) data type information of members is
provided for each class, so that the passivation/activation could be
written once and for all.

Which actually mean what…? Could you explain this activation/passivation
process a bit; I do not quite follow you… :slight_smile:

Thanks!

ako

But if using an array:

char array[sizeof(Instance)])
fstream inputfstream(“file”, ios_base::in | ios_base::binary)
inputfstream.read((char*) &array[0], sizeof(Instance)) );

that line works out just fine…

What to do here… Anyone??

Regards Arve

On Wed, 05 Sep 2001 15:20:23 +0200, in qdn.public.qnxrtp.devtools you
wrote:

< (…)

To clarify: the term “passivation/activation” is sometimes
used in the context of the Objective-C language, and basically means
saving and restoring objects to/from a file. The requirement for the
objects to be global is in fact not necessary, but for object with
pointers the method won’t work anyway. Furthermore, each object may
contain a hidden pointer (or other kind of information) that binds it
to its class, and this is why the method won’t work when the objects
are saved and restored by different programs.

For a simple solution, start with a function that takes two
arguments: a stream (pointer) and a (pointer to) an instance of a
class. Save the instance to the stream member by member (field by
field). You may choose text or binary representation; the first has an
advantage of producing readable files (but beware of the double type),
the second is faster but less portable. For each “nonstandard” field
type write another function that saves it to a stream. For pointers,
you have to save the information whether it was NULL, and then (if
not) the contents - using the same method. Since you are in C++, the
functions may be operators or methods of the same name, distinguished
by the type of the arguments. Define a(n) fstream superclass with new
saving methods or operators, see the definition of the “<<” operator,
for example. If you don’t know the exact type of the object a field
references, make the storing methods virtual. If your objects contain
circular references, provide an explicit solution.

For reading in, use the same approach. Each time an object is
to be read, invoke a constructor that creates an instance of the
proper class, and then read and assign its fields, one by one (it may
me the responsibility of the constructor itself). If you don’t know
the sequence the objects are stored in, let the saving methods provide
a class identifier in front of each stored object. If the objects are
already declared, you don’t have to invoke constructors.

If objects are read in by a different program, ensure it links
each class that may be needed. With the reading method explicitly
referencing the class constructors, it is guaranteed, but not always
desirable (because of the size of the program).

It is hard to write a general object saver/reader in C++ that
solves the type problem and the circular reference problem, but not
impossible, at some cost.

ako

On Wed, 05 Sep 2001 15:20:23 +0200, Arve Slenes <arve@datarespons.no>
wrote:

Supplement to the “…make the methods virtual”: provide each
class that needs it with a “save me to a stream” method.

ako

Thanks!

I’ll try using some methods such as serialize() and unserialize() …

-Arve

Andrzej Kocon wrote:

On Wed, 05 Sep 2001 15:20:23 +0200, Arve Slenes <> arve@datarespons.no
wrote:

Supplement to the “…make the methods virtual”: provide each
class that needs it with a “save me to a stream” method.

ako