473,886 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2063
"Clint Ruen" <go***@gross.ne t> wrote in message
news:2q******** *************** *********@4ax.c om...
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.ne t> 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
3666
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" file_names = "file2.txt" etc ...
6
4291
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 function called readline() because I would also like to do some processing on the line each time (ignoring comments and so on). I have:
1
1932
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 date. I need to view data for a selected week given the week number or date. need some controls on not allowing the ball number to go >49 or <0 and no duplicates.
2
2536
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 the link errors decreased from over 200 to 5. A great step! But one of my class derived from wxValidator can't be linked correctly though there is no any inline function in it. The error message: -----------------------
5
6038
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
1090
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 MessageQueue.cpp MessageFragmenter.cpp SingleMessage.cpp /tmp/ccsrT4jU.o(.gnu.linkonce.t._ZN9c_ServiceD1Ev+0xb): In function `c_Service::~c_Service ()': : undefined reference to `vtable for c_Service'
2
3604
by: raxitsheth | last post by:
Hello All... I am using Posix Thread. class Parent { public: virtual void* func(void *)=0;
9
13066
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 created and what they would contain: class base { virtual void display() { cout<<"base display"<<endl;
17
14733
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 vfptr i.e the pointer to vtable internally. What confuses me is if vtable is an array of pointer to virtual functions then as per the properties of the array all the entries inside the array must be same.i.e all the pointers should be of similar...
0
9799
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10770
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10429
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7139
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4627
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4235
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.