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 4 1939
"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.
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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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"...
|
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...
|
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...
|
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...
|
by: Jacek Dziedzic |
last post by:
Hi!
Consider the following program
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream in("test.txt");
|
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...
|
by: raxitsheth |
last post by:
Hello All...
I am using Posix Thread.
class Parent
{
public: virtual void* func(void *)=0;
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |