Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:04:53+00:00 2026-06-11T12:04:53+00:00

I have this code: class Class { public: virtual void first() {}; virtual void

  • 0

I have this code:

class Class {
public:
    virtual void first() {};
    virtual void second() {};
};

Class* object = new Class();
object->first();
object->second();
delete object;

that I compile with Visual C++ 10 with /O2 and have this disassembly:

282:    Class* object = new Class();
00403953  push        4  
00403955  call        dword ptr [__imp_operator new (4050BCh)]  
0040395B  add         esp,4  
0040395E  test        eax,eax  
00403960  je          wmain+1Ch (40396Ch)  
00403962  mov         dword ptr [eax],offset Class::`vftable' (4056A4h)  
00403968  mov         esi,eax  
0040396A  jmp         wmain+1Eh (40396Eh)  
0040396C  xor         esi,esi  
283:    object->first();
0040396E  mov         eax,dword ptr [esi]  
00403970  mov         edx,dword ptr [eax]  
00403972  mov         ecx,esi  
00403974  call        edx  
284:    object->second();
00403976  mov         eax,dword ptr [esi]  
00403978  mov         edx,dword ptr [eax+4]  
0040397B  mov         ecx,esi  
0040397D  call        edx  
285:    delete object;
0040397F  push        esi  
00403980  call        dword ptr [__imp_operator delete (405138h)]  

Note that at 00403968 the address of the object start (where vptr is stored) is copied into esi register. Then at 0040396E this address is used to retrieve the vptr and the vptr value is used to retrieve address of first(). Then at 00403976 the vptr is retrieved again and is used to retrieve the address of second().

Why is vptr retrieved twice? Could the object possible have its vptr changed in between calls or is it just an underoptimization?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:04:54+00:00Added an answer on June 11, 2026 at 12:04 pm

    Why is vptr retrieved twice? Could the object possible have its vptr changed in between calls or is it just an underoptimization?

    Consider:

    object->first();
    

    This call may destroy the object and create a new one in the same chunk of memory. Hence, after this call no assumptions can be made about the state. E.g.:

    #include <new>
    
    struct Class {
        virtual void first();
        virtual void second() {}
        virtual ~Class() {}
    };
    
    struct OtherClass : Class {
        void first() {}
        void second() {}
    };
    
    void Class::first() {
        void* p = this;
        static_assert(sizeof(Class) == sizeof(OtherClass), "Oops");
        this->~Class();
        new (p) OtherClass;
    }
    
    int main() {
        Class* object = new Class();
        object->first();
        object->second();
        delete object;
    }
    

    Compilers may optimize away unnecessary register loads if that function is inline and/or link-time code generation is used.


    As DeadMG and Steve Jessop noted the above code exhibits undefined behaviour. According to 3.8/7 of C++ 2003 Standard:

    If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if:

    • the storage for the new object exactly overlays the storage location which the original object occupied, and
    • the new object is of the same type as the original object (ignoring the top-level cv-qualifiers), and
    • the type of the original object is not const-qualified, and, if a class type, does not contain any non-static data member whose type is const-qualified or a reference type, and
    • the original object was a most derived object (1.8) of type T and the new object is a most derived object of type T (that is, they are not base class subobjects).

    The above code doesn’t satisfy requirement 2 from the above list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that describes a service: public class navigation_web extends Service {
I have this code that runs but never stops. class A { public static
I have defined some models like this (Entity Framework Code-First): public class A {
Say we have this code class A { public: A() : x(1) {} virtual
I have this code : class Event{}; class CustomEvent:public Event{}; class Handler { public:
Hello I have this code class Triplets { public: int nVal1; int nVal2; NodeT
I have this code: public class Window extends JFrame { public Window(){ ... JButton
I have this code: public class Area { Texture2D point; Rectangle rect; SpriteBatch _sB;
Okay, so i have this code i wrote: class Connection { public static StreamWriter
I have this code for my soundboard, public class soundboardActivity extends Activity { View

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.