Discussion Locked

Detect the location of the vptr

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Detect the location of the virtual pointer:

The June 18, C++ TechMail discusses how to detect the location of the virtual pointer. Have you used this technique to detect the vptr in your applications? Do you have a better solution? Tell us about it.

If you haven't subscribed to the C++ TechMail, there is no better time to take advantage of our free e-newsletter. Visit our e-newsletter subscription center to subscribe to this valuable TechMail today:

http://builder.com.com/techmails.jhtml?repID=u001

10 replies

by john torjo

First of all, even for serialization, you won't need the vptr. Why do you say that?

Second of all, the fact that the vptr is not at the beginning, does not necessary mean that the vptr is at the end.

And third, the code is very error-prone:
-it does not take base classes into account (what if the class is derived from another class)
- it does not account for whether the class has a vptr or not. For instance, in your example, if f() was not virtual, your code would print 'vptr is located at the end' even though there is no vptr whatsoever

- the class might be modified, and new members added (before what you thought was the first member). This would break the example shown.

I did not test your code under VC6 - since I can't right now - but I think the code is wrong anyway. I'll get back to you on that.

by danntk

First of all, let's confine the discussion to the real issue here, which is indeed the vptr's location, NOT serialization (serialization is a far more complex issue, especially in the presence of data member arrays, virtual base classes, embedded objects etc. but for the lack of space, I won't discuss it here)

The ISO standard says very little about the actual position of the vptr. In theory, you're correct: it might be located not just at the beginning or the end of an object. In practice, however, I'm not aware of a single implementation that doesn't allocate the vptr either at the beginning or the end of an object. So while the code isn't 100% bullet proof, I doubt that 100% bullet proof code can be written in standard C++ or that it's really necessary.

Your comment about the likelihood of code breaking due to adding data members is a bit, how to say, peculiar. We all know that the C++ object model requires recompilation and relinking when the binary layout of an object changes, either due to programmer's changes in the code or because one has switched to a new compiler, 64-bit addressing mode, different alignment setting etc. So surely no one expects old code that doesn't write new data members of a recently-modified class to function properly. Am I happy with this state of affairs? Not at all. Yet in the real world, that's how C+ programmers deal with serialization, unless they're using a non-standard framework such as DCOM or CORBA that eliminates at least some of the drudgery. I truly hope that in the next revision round, C++ will finally include built-in support for serialization.
BTW, the code was tested under C++ Builder, which uses the same object model of VC6 so it should work with the latter as well.If it doesn't, please keep us posted.

by john torjo

Hi Danny,

You say:"Your comment about the likelihood of code breaking due to adding data members is a bit, how to say, peculiar. We all know that the C++ object model requires recompilation and relinking when the binary layout of an object changes, either due to programmer's changes in the code or because one has switched to a new compiler, 64-bit addressing mode, different alignment setting etc."

But it's not an issue about recompilation, etc.
It's an issue that code trying to find the vptr of a class could break in case it's modified by someone else (by deriving from a base class or by inserting new data members before the first data member.

And you cannot know if a class has any virtual functions or not, therefore the code should be similar to:

if (p1==p2)
cout<<"vptr is located at the object's end OR DOES NOT EXIST"<

by danntk

Hi John,

Adding more data members or deriving from the class shouldn't affect the vptr's relative location. The new object will always have its vptr either at the beginning or the end. The compiler should take care of that and I'm not aware of any compiler that doesn't do so (unless you were referring to polymorphic pointers whose dynamic types and static types don't match -- in this case of course all bets are off but I didn't try to claim that the scanty code provided in the tip should handle such cases). For example:

struct A{
int n;
virtual void f() {}
};
struct B: public A{
int m;
};
int main(){
int n=sizeof(A); //8 bytes: vptr + n
n=sizeof (B); //12 bytes: vptr + n + m
}

If I had used a compiler that places the vptr at the end, it would simply have moved the vptr of B to offset 8 instead of 4. In both cases, there's only one vptr per object (embedded objects have their own vptr either at their end or their beginning but serializing complex object should be a recursive process so the code would still compile).

(to be continued...)

by john torjo

Hi Danny,

Here's why I meant derivation and extension DON'T pass the test (the code
you showed is VERY ERROR PRONE):

// File A.h

class A
{
public:
virtual void f() {}
int n;
};

// File A_find_vptr.cpp

bool has_A_vptr_at_beginning() {
A a;
char *p1=reinterpret_cast (&a);
char *p2=reinterpret_cast (&a.n);
return ( p1 != p2);
}

The point is that the function 'has_A_vptr_at_beginning()' is TIGHTLY COUPLED with A's

definition.
Now, some programmer (without knowing about has_A_vptr_at_beginning()), modifies A' like

this:

// [1] removing the virtual keyword - no vptr whatsoever.

class A
{
public:
void f() {}
int n;
};

or

// [2] adding a data memberBEFORE n

class A
{
public:
virtual void f() {}
char c;
int n;
};

// [3] deriving A from a base class

class Base {
int var;
};

class A : public Base
{
public:
virtual void f() {}
int n;
};

In case [1], depending onthe platform (the ALIGNMENT of data members)

has_A_vptr_at_beginning() might return true or false.

In case [2], has_A_vptr_at_beginning() will ALWAYS return true
(since it's clear that &a != &a.n)

In case [3], has_A_vptr_at_beginning() will ALWAYS return true, since
again &a != &a.n, since n is aligned AFTER Base's 'var' data member.

Besides, your code assumes 'n' is a public variable, breaking data hiding.

[more to come]

by john torjo

Now, lets bring multiple inheritance into the picture.
Did you know that we might have MULTIPLE vptrs?

// for VC6 and gcc3.2
// sizeof(First) = 8, sizeof(Second) = 8, sizeof(Both) = 16
//
// had there been ONLY ONE vptr, sizeof(Both)
// would have been 12 = sizeof(m_first) + sizeof(m_second) + 4 (vptr)
#include

struct First {
virtual void first_method() {}
int m_first;
};

struct Second {
virtual void second_method() {}
int m_second;
};

struct Both : public First, public Second {
};

int main(int argc, char* argv[])
{
std::cout
<< "first size= " << sizeof( First) << std::endl
<< "second size= " << sizeof( Second) << std::endl
<< "both size= " << sizeof( Both) << std::endl;
std::cin.get();
return 0;
}

This begs the question:
- if we might have multiple vptrs, why even care where the vptr is?
(even for serialization, your code tells you whether vptr is at the beginning
or at end; but if it's at the beginning, how do I know THEIR size in order to
ignore them - since it's not necessary 4, it could be more,
if there are multiple vptrs)

More to come - about serialization.

by danntk

As for serialization, the preferable solution is of course to use a library that will do the job for you (what a relief!). I would never recommend doing it on your own if you have a choice. But since there's no such standard library, the only optionfor portable and ISO-compliant code is to check the vptr's location in order to ensure that its position isn't overwritten during deserialization. When referring to serialization and deserialization I'm talking about dumping the entire object's content into a file as a stream of bytes and then writing it back to a default-constructed object. This way, the non-static data members get the values stored in the file without overwriting the vptr. Another important use of the vptr is for writing compilers, interpreters and simply for understanding the generated machine code (say for optimization or debugging).
Of course, you need additional info to ensure that the object DOES have a vptr. The code works under the presupposition that a has one. Again, the point is not to cover all possible cases. Two lines of C++ code can never accomplish that and I suspect that without resorting to assembly and compiler hacking, it's almost impossible to write a generic vptr detector. Finally, once you know where your compiler places its vptr, you don't really need to check every object. One or two examples of simple classes such as the ones included in the tip will suffice. And that was the whole idea behind the tip.

by john torjo

Now, lets answer the serialization:

First of all, some serialization libraries (there are many more though):
http://www.cuj.com/documents/s=7993/cujcexp1907reeves/
http://www.recursionsw.com/products/toolkits/whitepapers/STL%20in%20Action_A%20Universal%20

Streaming%20Service.pdf

For serialization purposes, the location of vptr is USELESS.

"the only option for portable and ISO-compliant code is to check the vptr's location in

order to ensure that its position isn't overwritten during deserialization"

Are you kidding me?
You're talking about dumping the entire object's content into a file as a stream of bytes

and writing it back as a default-constructed object.

Try doing that for a std::vector filled with, lets say 1000 elements.
Or for a pointer.
The above does NOT work even for most PODs (Plain Old Data).

So, this is broken even in C, but in C++ is a MAJOR flaw. What about OO-state?
What if you want to serialize a std::string? Your method will fail big-time.

Any non-trivial class will most-likely fail using the above.

Another problem is when you don't have a default constructor (of course, from all the

problems, this is the least of them).

Not to say that the location of the vptr still DOES NOT matter.

Even using your method, you should care about ALIGNMENT. So, you should do something like:
A a;
// no need for vptr at all
std::memcpy( &a.first_data_member, whatever_buffer, sizeof( a));

NOT
std::memcpy( &a, whatever_buffer, sizeof( a));

Since, for one: operator& might be overloaded, and secondly it's not necessary for this to

hold true:
(&a.first_data_member == &a)

Since such a code is incredibly error-prone, I will say that I wouldfire someone that used

such a serialization mechanism.

by john torjo

"First, read the serialized object from a file into an empty object, and then have its default constructor run. This way, the constructor will overwrite the bad vptr, replacing it with the correct value. "

First read it into an empty object, and then have its default constructor run. Are you kidding me? How do you have an "empty object"? What is that?

In order to have an object, you need to construct it first (or am I wrong)?
(if you were to somehow read it first, then the constructor might override some values - and that's the best case, since there could be much more problems)

Second of all, as I pointed out - there may be multiple vptrs (in case of multiple inheritance).

"you may wish to debug the assembly code generated by your compiler. Knowing where the vptr is located will enable you to calculate the addresses of virtual member functions."

What? Are you that deep? For what usage?
Anyway, as you probably know, the VC6+ compiler already does this for you - so you don't have to worry with such details.

by john torjo

Third:
See 'http://groups.google.com/groups?dq=&hl=en&lr=&ie=UTF-8&frame=right&th=73bcb71f888c7130&seekm=c638aac5.0307090816.11bd9536%40posting.google.com#link1'

You should have stated clearly if your intent was to have a dummy class for whichyou would test whether ALL vptrs are generated at the beginning or at the end. Or otherwise people might think that they would need to do it for their custom classes (which might be much more complicated than your example).

[if you were to use a dummy class, you could still get into trouble, since if a smart compiler could detect the dummy class' virtual functions are never used might not generate a vptr at all, therefore your function would be flawed]

But remember, even if you know if vptr is put at the beginning or at the end, you still won't know the size occupied by it (or them - in case of multiple inheritance). So, what's the use?

And last of all, if you want to know the location of the vptr FOR a given compiler, you'd better ask an expert on that compiler or the compiler vendor himself. It's not a task for everyone.

This discussion is closed to new replies.