473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Jul 22 '05 #1
4 2032
"Clint Ruen" <go***@gross.net> wrote in message
news:2q********************************@4ax.com...
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.
Jul 22 '05 #2
Clint Ruen wrote:
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

Jul 22 '05 #3

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
Jul 22 '05 #4
In message <2q********************************@4ax.com>, Clint Ruen
<go***@gross.net> writes
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.

--
Richard Herring
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt"...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
1
by: carl bloc | last post by:
Have this much done but need help with this bit of code: allow user to modify existing draw data. I think I need a counter to give week numbers so the user can select a week number rather than a...
2
by: Quansheng Liang | last post by:
Hello, I struggled with the problem of "undefined reference to `vtable ...`" while migrating a project from windows to linux. After searching the google I removed all the inline functions and now...
5
by: Jacek Dziedzic | last post by:
Hi! Consider the following program #include <fstream> #include <iostream> using namespace std; int main() { ifstream in("test.txt");
7
by: Karl Ebener | last post by:
Hi! I have created a program using several classes with inheritage. When I compile and link, I get the following error: :$ g++ service2.cpp Service.cpp Application.cpp Message.cpp...
2
by: raxitsheth | last post by:
Hello All... I am using Posix Thread. class Parent { public: virtual void* func(void *)=0;
9
by: kish_nand | last post by:
Could someone please explain me the concept behind virtual functions and vtables. I am little confused about this. Please refer to the following code and tell me how many virtual tables would be...
17
by: ypjofficial | last post by:
Hello All, I have read in many c++ literature that vtable is nothing but an array of pointer to virtual functions inside a class.And the class where the virtual function/s are declared stores the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.