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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:43:00+00:00 2026-05-19T15:43:00+00:00

I have structs like this: struct A { int a; virtual void do_stuff(A*a) {

  • 0

I have structs like this:

struct A
{
     int a;
     virtual void do_stuff(A*a)
     {
          cout << "I'm just a boring A-struct: " << a << endl;
     }
}

struct B
{
     A a_part;
     char * bstr;
     void do_stuff(B*bptr)
     {
          cout << "I'm actually a B-struct! See? ..." << bptr->bstr << endl;
     }
}

B * B_new(int n, char * str)
{
     B * b = (B*) malloc(sizeof(struct B));
     b->a_part.a = n;
     b->bstr = strdup(str);
     return b;
}

Now, when I do this:

char * blah = strdup("BLAAARGH");
A * b = (A*) B_new(5, blah);
free(blah);
b->do_stuff(b);

I get a segfault on the very last line when I call do_stuff and I have no idea why.
This is my first time working with virtual functions in structs like this so I’m quite lost. Any help would be greatly appreciated!

Note: the function calls MUST be in the same format as the last line in terms of argument type, which is why I’m not using classes or inheritance.

  • 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-19T15:43:00+00:00Added an answer on May 19, 2026 at 3:43 pm

    You’re mixing a C idiom (embedded structs) with C++ concepts (virtual functions). In C++, the need for embedded structs is obviated by classes and inheritance. virtual functions only affect classes in the same inheritance hierarchy. In your case, there is no relationship between A and B, so A‘s doStuff is always going to get called.

    Your segfault is probably caused because b is a really a B, but assigned to an A*. When the compiler sees b->doStuff, it tries to go to a vtable to look up which version of doStuff to call. However, B doesn’t have a vtable, so your program crashes.

    In C++, a class without virtual functions that doesn’t inherit from any other classes is laid out exactly like a C struct.

    class NormalClass
    {
          int a;
          double b;
    
     public:
          NormalClass(int x, double y);
    };
    

    looks like this:

    +------------------------------------+
    | a (4 bytes) | b (8 bytes)          |
    +------------------------------------+
    

    However, a class (or struct) with virtual functions also has a pointer to a vtable, which enables C++’s version of polymorphism. So a class like this:

     class ClassWithVTable
     {
          int a;
          double b;
    
       public:
           ClassWithVTable();
           virtual void doSomething();
     };
    

    is laid out in memory like this:

      +-----------------------------------------------------------+
      | vptr (sizeof(void *)) | a (4 bytes) | b (8 bytes)         |
      +-----------------------------------------------------------+
    

    and vptr points to an implementation-defined table called the vtable, which is essentially an array of function pointers.

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

Sidebar

Related Questions

If I have a code like this: struct A { virtual void f(int) {}
I have a struct like this: class Items { private: struct item { unsigned
I have code like this: template <typename T, typename U> struct MyStruct { T
In our code we used to have something like this: *(controller->bigstruct) = ( struct
I have this struct: struct Map { public int Size; public Map ( int
This is what I would like to do using templates: struct op1 { virtual
I have a C (not C++) struct that goes like this typedef struct mystruct{
I have a struct defined like follows as part of an object. I'm trying
I have seen many programs consisting of structures like the one below typedef struct
I have some generic types, like the following: public struct Tuple<T1, T2> { ...

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.