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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:34:50+00:00 2026-06-10T00:34:50+00:00

The following program output is always 1 1 1. In Inside the c++ object

  • 0

The following program output is always 1 1 1. In “Inside the c++ object model” book, it is mentioned that it will give offset. The purpose is also to find out object layout. But, I am confused with the output. Used g++ 4.5.2

class Test
{
    public:
        float a;
        float b;
        float c;
};

int main()
{
    float Test::*ptr = &Test::a;
    float Test::*ptr1 = &Test::b;
    float Test::*ptr2 = &Test::c;
    cout<<ptr<<endl;
    cout<<ptr1<<endl;
    cout<<ptr2<<endl;

    return 0;
}

Output:

1
1
1

Edit(Follow up question):
In the book it is mentioned that origin.y = 0 can be transformed to &origin + (Point3d::y-1) where origin is an object to Point3d and y is member variable of class Point3d. Though When I compiled it gave me compilation error.

  • 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-10T00:34:52+00:00Added an answer on June 10, 2026 at 12:34 am

    You wrote that you wanted to find the memory offset. While what FredOverflow writes is completely true, you should make an instance of your class Test if you want to know the address of a,b and c. For instance:

    Test t;
    float *ptr = &t.a;
    float *ptr1 = &t.b;
    float *ptr2 = &t.c;
    

    On my machine this yields the following three addresses:

    0x7fff564f8918
    0x7fff564f891c
    0x7fff564f8920
    

    And you will notice that they are 4 bytes (or sizeof(float)) apart and that the size of a Test is 12 bytes (using sizeof(Test)). Furthermore, the address of &t is 0x7fff564f8918 the same address of &t.a. That’s how the memory layout of an instance of the class Test is formed.

    You can also find the offset of members of a POD type by using offsetof().

    cout << offsetof(Test, a) << endl;
    cout << offsetof(Test, b) << endl;
    cout << offsetof(Test, c) << endl;
    

    Yields

    0
    4
    8
    

    Note that offsetof(Test, b) is essentially the same as

    (unsigned long long) &(((Test*) 0)->b) - (unsigned long long) (Test*) 0
    

    Answer to your followup question:

    That code will not work because of the same errors as previously mentioned. However, if you wanted to calculate the address of your y member of origin and assign it the value 0 it can be done thusly:

    class Point3d {
    public:
      float x, y, z;
    };
    
    Point3d origin;
    origin.y = 10;
    
    // We take the address of origin, which points to the first member, 
    // then add the offset to the member y.
    float *ptr = (float*) ((unsigned long long) &origin + offsetof(Point3d, y));
    cout <<  "Old value: " << *ptr << endl;
    *ptr = 0;
    cout <<  "New value: " << *ptr << endl;
    

    Yields the output:

    Old value: 10
    New value: 0
    

    Again remember that this is only possible because Point3d is a POD type.

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

Sidebar

Related Questions

Why is the output of the following program just int3 and not int3&4 ?
I have written the following C program. The output is 32. Why is this?
The following program I'm creating will allow the user will input values to create
The output from the following program is: Non-Static Static Non-Static Is this a compiler
For some reason the following C# Console program always outputs: 32 False wtf=0 What
I'm getting the following in the program output when I run my application. When
The following program gives output as I am Parameterized Ctor a = 0 b
I tried and got the following output: [object Object] I am familiar with adding
I have the following problem: I am writing a C++ program that has to
I am writing a program and am stuck in the following requirement: The output

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.