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

  • Home
  • SEARCH
  • 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 5990053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:11:19+00:00 2026-05-22T23:11:19+00:00

struct A { virtual void foo(); // unused and unimplemented virtual void bar ()

  • 0
struct A
{
  virtual void foo();  // unused and unimplemented
  virtual void bar () {}
};

int main ()
{
  A obj;        // ok
  obj.bar();  // <-- added this edition
  A* pm = (A*)malloc(sizeof(A)); // ok
  A* pn = new A; // linker error
}

For objects on stack it works fine. But for allocation on heap with new (not malloc), it gives linker error:

undefined reference to `vtable for A'
  • 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-22T23:11:20+00:00Added an answer on May 22, 2026 at 11:11 pm

    Firstly, this code is not compilable, since in C++ void * cannot be implicitly converted to A *. An explicit cast is required.

    Secondly, the example with malloc is completely irrelevant. malloc allocates raw memory, with has absolutely no relation to any specific types. In this case malloc knows noting about any A and it does not create an object of type A.

    For this reasons the real example for this question should look as follows

    struct A
    {
      virtual void foo();  // unused and unimplemented
      virtual void bar () {}
    };
    
    int main ()
    {
      A obj;        // ok
      A* pn = new A; // linker error
    }
    

    And the question is why the first declaration produces no liker error while the second one does.

    From the formal point of view, your program is invalid because it violates formal requirements of C++ language (specifically ODR). In practice, both declarations could or should produce the same error, since in both cases the object formally requires a pointer to VMT. In this case VMT cannot be created, since some functions are undefined. However, the first declaration simply slips through just because the compiler was able to optimize-out all references to VMT for the first declaration (and not for the second). It is also quite possible that the compiler was able to optimize-out the whole obj object, since it is not referenced anywhere else.

    In GCC (since you appear to be using GCC) it is easy to trigger the same error for the first declaration as well

    struct A
    {
      virtual void foo();  // unused and unimplemented
      virtual void bar () {}
    };
    
    int main ()
    {
      A obj; // linker error
      A *p = &obj;
      p->bar(); 
    }
    

    The above code will produce the same linker error in GCC even though the undefined function foo is still not used in this code.

    In other words, it is simply a matter of adding sufficient amount of code to make the compiler believe that the object’s VMT is needed. The difference in behavior between the declarations has nothing to do with C++ language in this case. It is just an implementation issue specific to your compiler.

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

Sidebar

Related Questions

Consider the following snippet: struct Base { virtual ~Base() {} virtual void Foo() const
I've got the following structure: struct A { A(); virtual ~A(); virtual void Foo()
struct TimerEvent { event Event; timeval TimeOut; static void HandleTimer(int Fd, short Event, void
void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
Consider the following snippet: struct ObjectInterface { virtual ~ObjectInterface() {} virtual void Print(std::ostream& target)
struct elem { int i; char k; }; elem user; // compile error! struct
typedef struct { nat id; char *data; } element_struct; typedef element_struct * element; void
Say I have a struct s with an int pointer member variable i. I
If I define a struct in C# using automatic properties like this: public struct
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char

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.