473,396 Members | 1,884 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,396 software developers and data experts.

virtual function table ptr stored to file and then again reread

Hello All,
I am defining a class with one virtual function and storing its first 4
bytes ie. the address of the virtual function table to a file.I am
again rereading the file in the same program and calling the virtual
function.The function gets called nicely but it shows the junk values
for the member variables of the class of which the function is a
member..below is the code

//Program start
#include "iostream.h"
#include "fstream.h"
struct vptrtable
{
void (*fptr)();
};

class one
{
int i;
public:
one(){i = 9;
}
virtual void fun()
{
cout<<"i is "<<i<<"\ninside fun\n";
}
};

int main()
{
one o;
fstream f("c:\\1.txt",ios::out);
//storing the vptr into the file
f.write((char*)&o,sizeof(int));
f.close();
int p=0;
fstream g("c:\\1.txt",ios::in);
//getting back the fptr from the file
g.read((char*)&p,sizeof(int));

vptrtable * v=(vptrtable*)p;
//calling the function.
v->fptr();
return 1;
}
//Program end
The output is
i is 1769172585
inside fun

i should be 9..but the function is getting called nicely..
what is going wrong here?
If it has to fail then why the function is getting called correctly?

Thanks and Regards,
Yogesh Joshi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 7 '06 #1
9 2405
yp*********@indiatimes.com wrote:
I am defining a class with one virtual function and storing its first
4 bytes ie. the address of the virtual function table to a file.I am
again rereading the file in the same program and calling the virtual
function.The function gets called nicely but it shows the junk values
for the member variables of the class of which the function is a
member..[..]
what is going wrong here?
If it has to fail then why the function is getting called correctly?
Undefined behaviour is just that, undefined. You cannot sensibly expect
anything certain from it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '06 #2
Ypj posted:
I am defining a class with one virtual function and storing its first 4
bytes ie. the address of the virtual function table to a file.

Platform-specific assumption.

f.write((char*)&o,sizeof(int));

First you say you want 4 bytes, next you say you want sizeof(int) bytes.

--

Frederick Gotham
Aug 8 '06 #3
yp*********@indiatimes.com schrieb:
Hello All,
I am defining a class with one virtual function and storing its first 4
bytes ie. the address of the virtual function table to a file.I am
again rereading the file in the same program and calling the virtual
function.The function gets called nicely but it shows the junk values
for the member variables of the class of which the function is a
member..below is the code
Your code invokes undefined behaviour. There is no (portable) way to
write the vtable to disk, using it later. Even if it would be possible,
it is a bad idea, since when you rebuild your programm, the compiler
could relocate the table, so the vtable pointer may become invalid.

[snipped code]
//Program end
The output is
i is 1769172585
inside fun

i should be 9..but the function is getting called nicely..
what is going wrong here?
If it has to fail then why the function is getting called correctly?
The simple explanation: Your code invokes undefined behaviour, it does
what it does.

The offtopic explanation: You created some sort of a copy of the
original object but left out the "int i". The function tries to access
some garbage value in memory.

--
Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 8 '06 #4

<yp*********@indiatimes.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Hello All,
I am defining a class with one virtual function and storing its first 4
bytes ie. the address of the virtual function table to a file.
No offence, but I hope you are well aware of what you are trying to do.
I also hope you already know there has been some talks about those who
write pointers to disk and read them back (first thing I remember is
Herb's talks).

Without looking at the piece of code below, I am guessing that you
forgot "this" pointer and read a junk memory, fortunately, somewhere
that does not cause a page fault (i.e. access violation, segfault).

You need correct "this" pointer to play with (super-fragile) vtable
properly. However, those extreme tricks are not reputable as far as
standard is concerned, as standard does not define vtable layout or
internal implementation details. In multiple-inheritance cases, vtable
layout may become extremely complex and even brings experts down their
knees.
I am
again rereading the file in the same program and calling the virtual
function.The function gets called nicely but it shows the junk values
for the member variables of the class of which the function is a
member..below is the code

//Program start
#include "iostream.h"
#include "fstream.h"
struct vptrtable
{
void (*fptr)();
};

class one
{
int i;
public:
one(){i = 9;
}
virtual void fun()
{
cout<<"i is "<<i<<"\ninside fun\n";
}
};

int main()
{
one o;
fstream f("c:\\1.txt",ios::out);
//storing the vptr into the file
f.write((char*)&o,sizeof(int));
f.close();
int p=0;
fstream g("c:\\1.txt",ios::in);
//getting back the fptr from the file
g.read((char*)&p,sizeof(int));

vptrtable * v=(vptrtable*)p;
//calling the function.
v->fptr();
return 1;
}
//Program end
The output is
i is 1769172585
inside fun

i should be 9..but the function is getting called nicely..
what is going wrong here?
If it has to fail then why the function is getting called correctly?

Thanks and Regards,
Yogesh Joshi
I see no (run-time) polymorphism here. In fact, I think I have
understood what you want, but the term "vtable" is usually used with
polymorphism.

Anyway, one short part of the answer to your question is (IMHO);
you jump to code, but you do not have a correct object and this pointer,
so the "i" is loaded with garbage. "this" pointer is required to find
correct address of member "i" (well, actually for virtual functions as
well) - it's about memory layout of the object.

If you derive a class from "class one" and call "fun" member function
polymorphically, your call will still go to one::fun rather than the
last override - and you will, of course, not haveany member of the class.

This is not portable, not standard, not good, too fragile, too evil (and
you have used it wrong).

Ismail

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 8 '06 #5
In article <11**********************@i42g2000cwa.googlegroups .com>,
yp*********@indiatimes.com writes
>Hello All,
I am defining a class with one virtual function and storing its first 4
bytes ie. the address of the virtual function table to a file.I am
again rereading the file in the same program and calling the virtual
function.The function gets called nicely but it shows the junk values
for the member variables of the class of which the function is a
member..below is the code
The real question is why yyou expect your code to work :-)
>
//Program start
#include "iostream.h"
Not a standard header
>#include "fstream.h"
Not a standard header
>struct vptrtable
{
void (*fptr)();
};
What is this for? And what makes you think that there is such a thing as
a virtual function pointer table. VFPT are just an implementation
detail.
>
class one
{
int i;
public:
one(){i = 9;
}
Please learn to write constructors using constructor initialiser lists:
one(): i(9) {}
virtual void fun()
Note that as a member function this function can only be called using an
object of type one (or via a reference or pointer to such an object
{
cout<<"i is "<<i<<"\ninside fun\n";
}
};

int main()
{
one o;
fstream f("c:\\1.txt",ios::out);
//storing the vptr into the file
f.write((char*)&o,sizeof(int));
No, all this does is to store the address of o as an array of char.
Whether that is, or is not the address of a vfpt is entirely
implementation dependant. However, much more important is your
assumptions that 1) the address of an object has the same format as the
address of a pointer to a table of function pointers. 2) that the size
of a pointer is the same as the size of an int.
f.close();
int p=0;
fstream g("c:\\1.txt",ios::in);
//getting back the fptr from the file
g.read((char*)&p,sizeof(int));
And here you go again, there is absolutely no requirement in C++ that
the bits of a pointer can be stored in a int lvalue.
>
vptrtable * v=(vptrtable*)p;
However even if all your other assumptions happen to work for the
implementation you are using all you have done here is to create a
pointer to a nonexistent object of type vptrtable
//calling the function.
v->fptr();
This has so many problems that it is hard to know where to start. As you
do have an object of type vptrtable v is pointing to some arbitrary
storage. It is pure coincidence that that expression seems to call a
member function of one. But as there is no object of type one anywhere,
directly or indirectly, in that expression statement you are deep in
undefined behaviour and are a little lucky that you have actually done
no damage.
return 1;
}
//Program end
The output is
i is 1769172585
inside fun

i should be 9..but the function is getting called nicely..
what is going wrong here?
If it has to fail then why the function is getting called correctly?
Begs the question. It wasn't called correctly so anything can happen
including reformatting your hard drive or causing some other chaos to
your computer.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 8 '06 #6
Hi All,
Thanks for the debate..

actually i tried this ..because I am learning COM programming and it is
based on the principle that the first 3 entry of the virtual function
table should always be same.So I was just trying to simulate this
behaviour in my own way by storing the virtual function table pointer
to a file and then later on accessing it..

Thanks for all your help..

Thanks and Regards,
Yogesh Joshi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 8 '06 #7

<yp*********@indiatimes.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Hi All,
Thanks for the debate..

actually i tried this ..because I am learning COM programming and it is
based on the principle that the first 3 entry of the virtual function
table should always be same.So I was just trying to simulate this
behaviour in my own way by storing the virtual function table pointer
to a file and then later on accessing it..

Thanks for all your help..

Thanks and Regards,
Yogesh Joshi

"First three entry of the vtable should always be the same", I don't
remember such a rule. Could that actually mean; "all objects in COM world
derive from IUnknown - directly or indirectly -, which has three methods"?

I recommend you to examine the your object's memory layout in your IDE's
"memory" window (with help of "watch" window as well). Unfortunately, your
question is not applicable in general, so bear with Visual C++ (or Studio)
IDE to investigate vendor-specific things (like vtable layout) further.

This is Microsoft specific question and I think people at Microsoft news
groups would help more.

Visual C++: microsoft.public.vc
ATL library (and COM stuff): microsoft.public.vc.atl

Cheers!
Ismail

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 9 '06 #8
"First three entry of the vtable should always be the same", I don't
remember such a rule. Could that actually mean; "all objects in COM world
derive from IUnknown - directly or indirectly -, which has three methods"?
Since this is not a topic to be discussed in this forum I will make it
look like typical C++ discussion so that other won't take any
objection..:)
Interface is an abstract class and abstract class is nothing but a
collection of pure virtual function...so internally the compiler
creates an array(i.e table) of pointers to virtual function (VTABLE)
and store the address of the array(vfptr) as a member of the abstract
class..so when you derive any class from this abstract class , it
inherits the vtable also and the compiler stores the addresses of the
overriden function to the vtable of the derived class..The functions
inside the vtable are always accessed by index..
So coming back to specific COM .. " Every interface should inherit from
the IUNKnown" means the first three entries of the vtable must be of
QueryInterface,AddRef and Release...
I recommend you to examine the your object's memory layout in your IDE's
"memory" window (with help of "watch" window as well). Unfortunately, your
question is not applicable in general, so bear with Visual C++ (or Studio)
IDE to investigate vendor-specific things (like vtable layout) further.

This is Microsoft specific question and I think people at Microsoft news
groups would help more..c++.moderated. First time posters: Do this! ]
Yes..I know..thats why I put the problem without using any platform
specific words..

Thanks for the discussion...

Yogesh Joshi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 9 '06 #9
Ismail Pazarbasi wrote:
"First three entry of the vtable should always be the same", I don't
remember such a rule. Could that actually mean; "all objects in COM
world derive from IUnknown - directly or indirectly -, which has three
methods"?

I recommend you to examine the your object's memory layout in your
IDE's "memory" window (with help of "watch" window as well).
This isn't neccessary at all. COM mandates a specific vtable layout and
calling convention. If one looks at the headers of the Windows SDK
which define the COM Objects of the runtime, one will see that every
interface is defined twice. Once as an abstract class in C++ and then
again as a C struct with a table of function pointers a the first and
only member. The OP should look at these (and the output of MIDL.EXE
which generates the same sort of setup) to learn about the vtables COM
expects.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 9 '06 #10

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

Similar topics

4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
0
by: Torsten Hensel | last post by:
Hi! I'm using ObjectiveGrid with C++ and I tried to understand the concept of virtual grids. I created a sample application that read table entries using GetStyleRowCol from an external source...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
7
by: Oleksii | last post by:
Hello, I'm rather new to the advanced topics, therefore I cannot explain the following myself. Could anyone give me a hint on this one? I'm trying to avoid link-time dependencies on (a test...
6
by: dharmadam | last post by:
Is it possible to pass a column name or the order of the column name in the DB2 table table function. For example, I want to update the address of a person by passing one of the address column name...
10
by: VM | last post by:
How can I limit the use of the PC's virtual memory? I'm running a process that basically takes a txt file and loads it to a datatable. The problem is that the file is over 400,000 lines long (77...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.