Does the vtable poiinter get reset with an ifstream read function????

C

Clint Ruen

Hello all,

I have written out a data structure using the binary flag on an
ofstream. The struct/class is something like this

class SomeData
{

public:
int data1;
float data 2;
}

the call was something like this

fout.write( ( char* )( &theData), sizeof( theData ) );

Now. when I read this back in with an ifstream...

fin.read( (char*)( &newData) , sizeof( newData ) );

I can fill up these classes/structs. However, when I make the class
virtual( by adding a virtual function ), the write function stores the
pionter to the vtable as the first four bytes, then the consecutive
member data.

When I read the data back in, the data seems to be read in properly,
ignoring those bytes that store the pointer to the vtable, and update
the vtable pointers correctly.

Is this behaviour correct? Or am I dancing with undefined behaviour
and getting lucky? Is there an RTFM for this? I guess that the
ifstream knows that the class it's reading in is a virtual class,
hence ignoring that pointer, then filling it in with correct data???
Is this a compiler implementation, is it standard? help, I can't
believe this works but it does :(????

--clint
 
A

Andrew Koenig

I can fill up these classes/structs. However, when I make the class
virtual( by adding a virtual function ), the write function stores the
pionter to the vtable as the first four bytes, then the consecutive
member data.

When I read the data back in, the data seems to be read in properly,
ignoring those bytes that store the pointer to the vtable, and update
the vtable pointers correctly.

Is this behaviour correct? Or am I dancing with undefined behaviour
and getting lucky?

You are dancing with undefined behavior and getting lucky.
 
K

Krishanu Debnath

Clint said:
Hello all,

I have written out a data structure using the binary flag on an
ofstream. The struct/class is something like this

class SomeData
{

public:
int data1;
float data 2;
}

the call was something like this

fout.write( ( char* )( &theData), sizeof( theData ) );

Now. when I read this back in with an ifstream...

fin.read( (char*)( &newData) , sizeof( newData ) );

I can fill up these classes/structs. However, when I make the class
virtual( by adding a virtual function ), the write function stores the
pionter to the vtable as the first four bytes, then the consecutive
member data.

When I read the data back in, the data seems to be read in properly,
ignoring those bytes that store the pointer to the vtable, and update
the vtable pointers correctly.
But are you sure that you always read the data in same run? Virtual
table may be in different location for each class in different run !

The way (non portable) one can manage it .. create a dummy object of that
class ( at restore time) assign the vtable of the dummy object to the
restored
object assuming the vtable is kept in first four byte.

Place of vtable pointer is different across the compiler. It's first
four byte
for gcc/Sun CC compiler but MSVC++ store is in last four byte.


Krishanu
 
C

Clint Ruen

But are you sure that you always read the data in same run? Virtual
table may be in different location for each class in different run !

The way (non portable) one can manage it .. create a dummy object of that
class ( at restore time) assign the vtable of the dummy object to the
restored
object assuming the vtable is kept in first four byte.

Place of vtable pointer is different across the compiler. It's first
four byte
for gcc/Sun CC compiler but MSVC++ store is in last four byte.


Krishanu
Yes, this is the crazy part. To convinve myself I compliled in
Release, only doing the reads( not write/read ), rebooted my machine
and the results still came out correct. Keep in mind I am using
fstreams and not C file input. I think I will try another machine
with no notion of Dev Studio....

--Clint
 
R

Richard Herring

Clint Ruen said:
Hello all,

I have written out a data structure using the binary flag on an
ofstream. The struct/class is something like this

class SomeData
{

public:
int data1;
float data 2;
}

the call was something like this

fout.write( ( char* )( &theData), sizeof( theData ) );

Now. when I read this back in with an ifstream...

fin.read( (char*)( &newData) , sizeof( newData ) );

I can fill up these classes/structs. However, when I make the class
virtual( by adding a virtual function ), the write function stores the
pionter to the vtable as the first four bytes, then the consecutive
member data.

When I read the data back in, the data seems to be read in properly,
ignoring those bytes that store the pointer to the vtable, and update
the vtable pointers correctly.

Is this behaviour correct? Or am I dancing with undefined behaviour
and getting lucky? Is there an RTFM for this? I guess that the
ifstream knows that the class it's reading in is a virtual class,

How could it? You're passing it a char *, not a virtual anything.
hence ignoring that pointer, then filling it in with correct data???
Is this a compiler implementation, is it standard? help, I can't
believe this works but it does :(????

You're just very lucky.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top