Detecting the vptr’s Location

The precise location of the vptr (the pointer to the class’s table of virtual functions’ addresses) is implementation-dependent. Some compilers, e.g., Visual C++ and C++ Builder, place it offset 0, before the user-declared data members. Other compilers, such as GCC and DEC CXX, place the vptr at the end of the class, after all the user-declared data members. Normally, you wouldn’t care about the vptr’s position. However, under certain conditions, for example, in applications that dump an object’s content to a file so that it can be retrieved afterwards, the vptr’s position matters. To detect it, first take the address of an object that. Then compare it to the address of the first data member of that object. If the two addresses are identical, it’s likely that the vptr is located at the end. If, however, the member’s address is higher than the object’s address, this means that the vptr is located at the object’s beginning. To detect where your compiler places the vptr, run the following program:

 class A{public: virtual void f() {} int n;};int main(){ A a; char *p1=reinterpret_cast  (&a); char *p2=reinterpret_cast  (&a.n); if (p1==p2)  cout<<"vptr is located at the object's end"<
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS