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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:24:20+00:00 2026-06-17T12:24:20+00:00

I was given this code to test my cpp understanding, and I am quite

  • 0

I was given this code to test my cpp understanding, and I am quite confused:

#include "stdafx.h"
#include <iostream>
#include <cstddef>

using namespace std;

class A
{
public:
    A() : m_x(0) { }

public:
    static ptrdiff_t member_offset(const A &a)
    {
        const char *p = reinterpret_cast<const char*>(&a);
        const char *q = reinterpret_cast<const char*>(&a.m_x);

        return q - p;
    }

private:
    int m_x;
};

class B
    : public A
{
public:
    B() : m_x('a') { }

public:
    static int m_n;

public:
    static ptrdiff_t member_offset(const B &b)
    {
        const char *p = reinterpret_cast<const char*>(&b);
        const char *q = reinterpret_cast<const char*>(&b.m_x);

        return q - p;
    }

private:
    char m_x;
};

int B::m_n = 1;

class C
{
public:
    C() : m_x(0) { }
    virtual ~C() { }

public:
    static ptrdiff_t member_offset(const C &c)
    {
        const char *p = reinterpret_cast<const char*>(&c);
        const char *q = reinterpret_cast<const char*>(&c.m_x);

        return q - p;
    }

private:
    int m_x;
};

int _tmain(int argc, _TCHAR* argv[])
{
    A a;
    B b;
    C c;
    std::cout << ((A::member_offset(a) == 0) ? 0 : 1);
    std::cout << ((B::member_offset(b) == 0) ? 0 : 2);
    std::cout << ((A::member_offset(b) == 0) ? 0 : 3);
    std::cout << ((C::member_offset(c) == 0) ? 0 : 4);
    std::cout << std::endl;

    return 0;
}

The answer is 0204.
I have understand the first 3cases, but not the last one. The difference between the last and first is a virtual desctructor. Is this related? If yes, how?

  • 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-17T12:24:21+00:00Added an answer on June 17, 2026 at 12:24 pm

    The code example has an implementation defined behavior. The output for any of the cases cannot be guaranteed. It is not guaranteed that members of a class are always placed in contiguous memory locations.There can be padding bytes added in between them. And whether or not padding is added is left out as an implementation detail. Your suspicion of virtual playing a role just might be true[Note 1:]. But the important thing to note is even without the virtual the output is not guaranteed.

    Reference:
    C++11: 9.2 Class members [class.mem]

    14) Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so
    that later members have higher addresses within a class object. The order of allocation of non-static data
    members with different access control is unspecified (11). Implementation alignment requirements might
    cause two adjacent members not to be allocated immediately after each other; so might requirements for
    space for managing virtual functions (10.3) and virtual base classes (10.1).


    [Note 1]:
    Dynamic dispatch itself is a implementation defined mechanism but most(read all known) implementations use the virtual table and pointer mechanism to implement it.
    In case of a polymorphic class(which does not derive from any other class) usually the virtual pointer is stored as the first element of class. So it is reasonable to assume that this is what happens behind the scenes in the last case when you run the code sample on your environment.

    Online sample:

    #include<iostream>
    using std::cout;
    using std::endl;
    
    class B;
    typedef void (*HANDLE_DOSOMETHING)(B *const, int q);
    
    class B
    {
    public:
      virtual void doSomething(int q)
      {
          std::cout<<"B::doSomething()"<<q<<endl;
      }
      void dummy()
      {
          HANDLE_DOSOMETHING *f1ptr = NULL;
          int                *vtbl  = NULL;
          int                *vptr  = (int *)this; // address of the object
    
          vtbl = (int *)*vptr; //address of the VTABLE
    
          f1ptr = (HANDLE_DOSOMETHING *)&(vtbl[0]); //address of the 1st virtual function
          (*f1ptr)(this, 55);
       }
    };
    int main()
    {
        B objb;
        objb.dummy();
        return 0;  
    }
    

    Output:

    B::doSomething()55
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Code: #include <iostream> using namespace std; #define ADD(x,y) ((x)+(y)) int main( int argc, char**
Given this code: #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <boost/regex.hpp> using
This compiles when using clang -std=gnu++11 -c test.cpp : void test() { [[random text
Ok, here's what I'm trying to do... given this razor code @using(Html.WriteLater()) { output
I have this piece of code which compiles and works as expected: #include <iostream>
Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
Given this code, tableButton = new JButton(new ImageIcon(80F_FG2015.GIF)); how would I get that String
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
Given this code: List<string> things = new List<string>(); foreach (string thing in things) {

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.