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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:46:40+00:00 2026-05-23T23:46:40+00:00

Take the typical demonstration of how pointer values can change through casting : struct

  • 0

Take the typical demonstration of how pointer values can change through casting:

struct B1 { int x; };
struct B2 { int y; };
struct D  : B1, B2 { };

int main() {
   D d;
   cout << (B1*)&d << " " << (B2*)&d << " " << &d;
}

// Typical output:
// 0xbf814ab4 0xbf814ab8 0xbf814ab4

I got to thinking; that offset probably does not exist when B1 has no members, so I checked and it’s true (at least in this case; not sure how standards-guaranteed this behaviour is):

struct B1 { };
struct B2 { };
struct D  : B1, B2 { };

int main() {
   D d;
   cout << (B1*)&d << " " << (B2*)&d << " " << &d;
}

// Typical output:
// 0xbf6ad95b 0xbf6ad95b 0xbf6ad95b

But then, sizeof(T) cannot be 0, so sizeof(B1) is still 1.

It strikes me that this “discrepancy” may potentially cause serious error-prone code where a programmer assumes that (char*)(B2*)&d - (char*)(B1*)&d == (ptrdiff_t)sizeof(B1).

Is my analysis accurate?

  • 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-23T23:46:41+00:00Added an answer on May 23, 2026 at 11:46 pm

    The B1 and B2 objects are subobjects of d. The sizeof operator gives information about the size of a complete object, not a subobject.

    The Standard allows but does not require a base class subobject to occupy no memory. So on another compliant implementation, you could find in your second example that the subobjects have different addresses after all.

    1.8p5: Unless it is a bit-field, a most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class subobjects may have zero size. An object of trivially copyable or standard-layout type shall occupy contiguous bytes of storage.

    1.8p6: Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. Two distinct objects that are neither bit-fields nor base class subobjects of zero size shall have distinct addresses.

    And the only "safe" uses of pointer arithmetic are:

    • On pointers to elements of the same array
    • The guarantee that the address of a subobject y of complete object x is between &x inclusive and &x+1 exclusive.

    Subtracting two void* pointers is ill-formed. You probably meant to reinterpret_cast<char*> or something. (Another sign that the code is very risky.)

    5.7p4: For the purpose of these operators [binary + and -], a pointer to a nonarray object behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.

    5.7p6: When two pointers to elements of the same array are subtracted, the result is the difference of the subscripts of the two array elements. … Unless both pointers point to elements of the same array object, the behavior is undefined.

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

Sidebar

Related Questions

EditorFor() can take an object additionalViewData parameter which the typical method to populate is
Take this code: <?php if (isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; } if
Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by
Take the following two lines of code: for (int i = 0; i <
I have a typical Subversion set-up: /trunk /branches/client-one /branches/client-two /branches/client-three Trunk holds main development
Here is the typical string I am working with: · Identify & document site-related
Take a look at the ssl_requirement plugin. Shouldn't it check to see if you're
Take a .Net Winforms App.. mix in a flakey wireless network connection, stir with
Take the following string as an example: The quick brown fox Right now the
Take the following C# class: c1 { event EventHandler someEvent; } If there are

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.