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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:07:10+00:00 2026-06-15T18:07:10+00:00

At first glance, this looks like undefined behavior… #include <iostream> struct SomeBaseClass { //

  • 0

At first glance, this looks like undefined behavior…

#include <iostream>

struct SomeBaseClass
{
    // ...
};

struct MyFakerClass
{
    SomeBaseClass base;
    void foo() { std::cout << "hello" << std::endl; }
};

int main()
{
    MyFakerClass c;
    MyFakerClass *p = static_cast<MyFakerClass *>(static_cast<void *>(&c.base));
    p->foo();
}

… but if the first field of an object is guaranteed to have the same address as the object, then this must be safe, right? Or is there some other rule that intervenes?

  • 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-15T18:07:11+00:00Added an answer on June 15, 2026 at 6:07 pm

    A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. — end note ]

    (C++11, §9.2, ¶21)

    So, this is safe as long as a class is “standard-layout”, i.e.:

    A standard-layout class is a class that:

    • has no non-static data members of type non-standard-layout class (or array of such types) or reference,
    • has no virtual functions (10.3) and no virtual base classes (10.1),
    • has the same access control (Clause 11) for all non-static data members,
    • has no non-standard-layout base classes,
    • either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
    • has no base classes of the same type as the first non-static data member.

    (C++11, §9, ¶7)

    All the requirements about the virtual stuff are because of the vptr: as said above, many compilers put it as a first hidden member, but they are allowed to put it anywhere or, if they are able, to omit it at all or implement virtual dispatch in other ways, so virtual in general alters the layout of the class in an unspecified way.

    The “same access control clause” is because

    The order of allocation of non-static data members with different access control is unspecified.

    (C++11, §9.2, ¶15)

    I suppose that this may be to allow compilers to reorder/group the members of a class by access-control (i.e. inside the compiler the IR of a class may contain a list for the members under each access-control specifier, which does not allow to keep the original ordering – all the interleaved public/private/protected sections would be grouped and e.g. the private members may be put in front of the class by default).

    The no non-standard-layout members/base classes “recursive” clauses are to ensure that the class is standard-layout as a whole (base classes and members are a part of it).

    Additional details on why the rest of this stuff can affect the layout of the class may be found in §9.2.


    Notice that this is a weaker condition than the class being POD (as I incorrectly said in the comment above), since a class is POD if it is both standard layout and trivial (§9 ¶10)

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

Sidebar

Related Questions

I know at first glance (due to the title) this looks like one of
(At first glance this may look like a duplicate of Different execution plan when
This probably sounds like a terrible idea at first glance, but here is my
I just found this: http://www.emadar.com/fpc/lockfree.htm at first glance it looks fine. Is anybody using
This looks like a long question because of all the context. There are 2
I have a code that looks like (more or less) : public void INeedHolidaysNow(double[,]){
Suppose I have a table, Document, that looks something like this: _______________________ | Document
First of all, this isn't for a keylogger, it's for an input in a
first take a look on this picture from localScope app : i have 2
I have factory that looks something like the following snippet. Foo is a wrapper

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.