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 6668231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:01:10+00:00 2026-05-26T03:01:10+00:00

Is there any compiler independent and syntactically elegant way to set a vtable pointer

  • 0

Is there any compiler independent and syntactically elegant way to set a vtable pointer in an object allocated with malloc?

I cannot use new directly as I need to be able to control the flow of memory releases on demand which requires the use of void ptrs to hold memory locations in a memory manager until there is ample time to release.

class AbstractData
{
public:
   AbstractData() {}
   virtual ~AbstractData() {}

protected:
   virtual void SetData(int NewData) =0;
   virtual int GetData() const =0;
};

class ConcreteData : public AbstractData
{
protected:
   int Data;

public:
   ConcreteData() {}
   ~ConcreteData() {}

   void SetData(int NewData);
   int GetData() const;

};

void ConcreteData::SetData(int NewData) {Data=NewData;}

int ConcreteData::GetData() const 
{return Data;}

int main(int argc, char* argv[])
{
    int OBJ_NUMBER = 4;
    ConcreteData* Test = (ConcreteData*)malloc(OBJ_NUMBER*sizeof(ConcreteData));

    if (!Test)
        return -1;        

    for (int x = 0; x < OBJ_NUMBER; x++)
       Test[x] = ConcreteData();

    Test[0]->GetData(); //Constructor was never called, vptr never initialized, crash

    free(Test);
    Test = NULL;
}

I was hoping the local copy would have an initialized vtable pointer, but alas it does not.
I know you can do a compiler dependent dereference to the offset of the vptr if you know where it is, but this solution is compiler dependent and inelegant to use across many allocations. Example works with MSVC++ 8.0

int main(int argc, char* argv[])
{
    int OBJ_NUMBER = 4;
    ConcreteData* Test = (ConcreteData*)malloc(OBJ_NUMBER*sizeof(ConcreteData));

    if (!Test)
        return -1;        

    ConcreteData StealVPtr();

    int* VPtr = *(int**)StealVPtr; 

    for (int x = 0; x < OBJ_NUMBER; x++)
       *(int**)Test[x] = VPtr;

    Test[0]->GetData(); //VPtr initialized in compiler dependent way

    free(Test);
    Test = NULL;
}

Alternatively, placement new could be used, but once again it looks syntactically inelegant and can cause array offsetting problems of types with destructors when it adds the array count in front of the ptr.

int main(int argc, char* argv[])
{
    int OBJ_NUMBER = 4;
    ConcreteData* Test = (ConcreteData*)malloc(OBJ_NUMBER*sizeof(ConcreteData));

    if (!Test)
        return -1;        

    for (int x = 0; x < OBJ_NUMBER; x++)
    {
        if (!(ConcreteData* PlcTest = new(Test[x]) ConcreteData()))
        {
           free(Test);
           Test = NULL;
           return -1;
        }
    }

    PlcTest[0]->GetData(); //Constructor was invoked and VPtr was initialized

    for (int x = OBJ_NUMBER-1; x >= 0; x--)
        PlcTest[x].~ConcreteData();

    PlcTest = NULL;

    free(Test);
    Test = NULL;
}

Are these really the only ways to initialize the VTable ptr/call constructors on objects using malloc?

  • 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-05-26T03:01:11+00:00Added an answer on May 26, 2026 at 3:01 am

    You may not know that you can override global new and delete. Pay attention, you also need to override new[] and delete[] to be complete

    Here an example:

    void * operator new( size_t size ) 
    {
        return super_malloc( size );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to suppress Compiler warnings ?? Thanks
Is there any way to get the version and vendor of the compiler used
Is there any compiler setting or other way to force an int to be
Is there any way to make static libraries built in Microsoft Visual Studio independent
Is there any compiler that has a directive or a parameter to cast integer
I'm currently developing a cross-platform C application. Is there any compiler macro which is
Is there any work being done to create a C# compiler to produce native
I am looking for a Java equivalent to .NET's Snippet Compiler. Is there any
Is there any way to pre compile stored procedures in SQL Server? My requirement
Are there any extensible interpreted programming languages written in standard, platform-independent C or C++?

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.