Advertisement

Some question in memory [ Data Structure ]

Started by September 13, 2017 02:06 AM
13 comments, last by ericrrichards22 7 years, 2 months ago

For the same code :


class B{
public:
    int age;
    int code;
    B(){
    cout << "Age | " << &age << endl;
    cout << "Code | " << &code << endl;
    }

};

int main()
{
   B obj;
   cout << "Object | " << &obj << endl;
   return 0;
}


Output :
Object | 0x6afef8
this object is just a pointer right ? how this pointer points to all the member classes ? 
how to get the offsets of the class member ?
 

27 minutes ago, User134 said:

Suppose i have this code :
 



class B{
public:
    int age;
    int code;
    B(){
    cout << "Age | " << &age << endl;
    cout << "Code | " << &code << endl;
    }

};

int main()
{
   B obj;
   cout << "Object | " << &obj << endl;
   return 0;
}


Output :
Age | 0x6afef8
Code | 0x6afefc
Object | 0x6afef8

here why the object address is the same for the age address ?

When you print out the address of the class, it will always point to it's initial member. I believe this is a standard since 2011.

Programmer and 3D Artist

Advertisement
Just now, Rutin said:

When you print out the address of the class, it will always point to it's initial member. I believe this is a standard since 2011.

what do you mean by standard since 2011 ?

1 hour ago, User134 said:

what do you mean by standard since 2011 ?

I believe he is referring to C++11, the 2011 version of the C++ standard, that modern C++ compilers are supposed to comply with.

Historically, C++ was a little wild west, with different compilers often producing all manner of non-standard behavior, and kinda almost not really working the same way.  So you'd compile some code with Borland, which would produce different assembly than MSVC++, which would produce different assembly than G++.  They might layout memory differently, they might support different preprocessor directives, all kinds of mess.

Eventually people realized this was awful, and got together to write standards about how memory ought to be laid out, amongst myriad other things that Bjarne Stroustrup had left sorta vague.

Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22

This topic is closed to new replies.

Advertisement