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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:06:57+00:00 2026-06-13T07:06:57+00:00

If I run the following code, I get different addresses printed. Why? class Base1

  • 0

If I run the following code, I get different addresses printed. Why?

class Base1 {
    int x;
};

class Base2 {
    int y;
};

class Derived : public Base1, public Base2 {

};

union U {
    Base2* b;
    Derived* d;
    U(Base2* b2) : b(b) {}
};

int main()
{
    Derived* d = new Derived;

    cout << d << "\n";
    cout << U(d).d << "\n";

    return 0;
}

Even more fun is if you repeatedly go in and out of the union the address keeps incrementing by 4, like this

int main()
{
    Derived* d = new Derived;

    cout << d << "\n";
    d = U(d).d;
    cout << d << "\n";
    d = U(d).d;
    cout << d << "\n";

    return 0;
}

If the union is modified like this, then the problem goes away

union U {
    void* v;
    Base2* b;
    Derived* d;
    U(void* v) : v(v) {}
};

Also, if either base class is made empty, the problem goes away.
Is this a compiler bug? I want it to leave my pointers the hell alone.

  • 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-13T07:06:58+00:00Added an answer on June 13, 2026 at 7:06 am

    If I run the following code, I get different addresses printed. Why?

    Because the Base2 sub-object of the Derived object isn’t at the start of the Derived object. So the addresses are different. When the compiler performs an implicit cast from a Derived* to a Base2*, it needs to adjust the address.

    Given the definitions of the Base1 and Base2 classes, both sub-objects of the Derived class cannot possibly be at the starting address of a Derived object – there’s no room at that address for both sub-objects.

    Say you had this code:

    Derived* d = new Derived;
    
    Base1* pb1 = d;
    Base2* pb2 = d;
    

    How would it be possible for pb1 and pb2 to point to the same address? pb1 has to point to a Base1::x item, and pb2 has to point to a Base2::y item (and those items have to be distinct).

    Even more fun is if you repeatedly go in and out of the union the address keeps incrementing by 4

    Because you’re reading from the union’s d member after writing the b member, which is undefined behavior (you’re essentially performing something like a reinterpret_cast<Derived*>() on a Base2*).

    I want it to leave my pointers the hell alone.

    Not if you want a Base2* pointer. Multiple inheritance makes things more complex – that’s why many people suggest avoiding it unless absolutely necessary.

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

Sidebar

Related Questions

When I run the following code I get B as expected: class A {
I run the following code from command line: public class MemoryTest { public static
When I run the following code: public int[] finalResult = new int[dimension]; public float[]
If I run the following code on my HTC magic phone I get a
When I run the following code, I get the exception below: ''# NOTE: ExcelApp
I'm trying to run the following code, but get an error in the gameLoop
When I try to run a routine I get the following error: Error Code:
When I run the following code on one machine I get tomcat implementation of
I am trying to run the following code: public BigDecimal valuate(String searchTerms, String categoryPath)
If I run the following code from a terminal, I get a helpful error

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.