473,385 Members | 1,275 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Memory layout of class

Hi,
What is the memory layout of an object, and the derived object.
For example :

class A
{
private:
int prv_data;
protected:
int pro_data;
public:
int pub_data;
A(){prv_data = 10;}
void Aprint();
};

class B : public A
{
int pro_data;
public:
void Bmember1();
void Bprint();
};

Does it depend on the compiler ?

int main()
{
B b;
int *ptr;

ptr = (int *)&b;
*ptr = 1000;
ptr++;
*ptr = 2000;
ptr++;
*ptr = 3000;
ptr++;
*ptr = 4000;

return 0;
}

is setting only the private element of class A to 1000, rest are not
being set.
Jun 29 '08 #1
3 5267
Peskov Dmitry wrote:
Hi,
What is the memory layout of an object, and the derived object.
For example :

class A
{
private:
int prv_data;
protected:
int pro_data;
public:
int pub_data;
A(){prv_data = 10;}
void Aprint();
};

class B : public A
{
int pro_data;
public:
void Bmember1();
void Bprint();
};

Does it depend on the compiler ?

int main()
{
B b;
int *ptr;

ptr = (int *)&b;
*ptr = 1000;
ptr++;
*ptr = 2000;
ptr++;
*ptr = 3000;
ptr++;
*ptr = 4000;

return 0;
}

is setting only the private element of class A to 1000, rest are not
being set.
See
http://www.research.att.com/~bs/bs_faq2.html#layout-obj

Jun 29 '08 #2
On Jun 29, 1:52*pm, Peskov Dmitry <vamsi.kom...@gmail.comwrote:
Hi,
* What is the memory layout of an object, and the derived object.
For example :

class A
{
* *private:
* * * * int prv_data;
* *protected:
* * * * int pro_data;
* *public:
* * * * int pub_data;
* * * * A(){prv_data = 10;}
* * * * void Aprint();

};

class B : public A
{
* * int pro_data;
* * public:
* * * * void Bmember1();
* * * * void Bprint();

};

Does it depend on the compiler ?

int main()
{
* * * * B b;
* * * * int *ptr;

* * * * ptr = (int *)&b;
* * * * *ptr = 1000;
* * * * ptr++;
* * * * *ptr = 2000;
* * * * ptr++;
* * * * *ptr = 3000;
* * * * ptr++;
* * * * *ptr = 4000;

* * * * return 0;

}

is setting only the private element of class A to 1000, rest are not
being set.
The memory layout of a class is compiler dependent. The following
article should help:

http://www.eventhelix.com/realtimema...erformance.htm

Memory layout for inherited classes is explained in:

http://www.eventhelix.com/realtimema...formance2..htm

--
http://www.EventHelix.com/EventStudio
Sequence diagram based systems engineering tool
Jun 30 '08 #3
In article <2f35231f-96fc-406a-a078-d77fbb1799d6
@i18g2000prn.googlegroups.com>, va**********@gmail.com says...
Hi,
What is the memory layout of an object, and the derived object.
[ ... ]
Does it depend on the compiler ?
Partly. If there's no private/public/protected label between them, items
in an object are required to be placed at increasing addresses. When
there is an intervening private/protected/public label, there is no
guarantee about relative ordering from the standard.

As far as base classes go, IF you're dealing with single inheritance
quite a few compilers put the base class at the beginning of the derived
class. This makes it trivial to convert an object to its base class
object (i.e. the same address, treated as a pointer to the base class,
works). Obviously when you have multiple inheritance, the compiler can
only place one base class at the beginning of the derived class objects.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 30 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
1
by: ankit.ankit | last post by:
Referring to the following links. 1....
10
by: Steven T. Hatton | last post by:
I just read something interesting in one of the PDFs located here: http://www.cs.wustl.edu/~schmidt/C++/ Sorry, I don't recall which file it was, and I'm too lazy to dig it up again ;) It says...
10
by: Luke | last post by:
Hi. I am trying to make correct layout, here is an example of (dynamically generated content via jsp): http://localhost/www/layout.htm Most outer div is positioned absolute (if not then it...
7
by: toton | last post by:
Hi, I have a STL vector of of characters and the character class has a Boost array of points. The things are vector<Characterchars; and class Character{ private: array<Point,Npoints; }; Now...
11
by: Henryk | last post by:
I have something like class Params { public: const static char nOne = 1; const static int nTwo = 2; const static char nThree = 3; }; This is just a wrapper for globally used parameters in...
1
by: stasgrom | last post by:
Guys, I have this situation: the class C is defined as follow: class C: public A, public B {...}; Now when I create an object of class C, I can see that the compiler (VS 8.0) decides to layout...
5
by: .rhavin grobert | last post by:
let say i have struct SA { long l1; long l2: long l3; }; struct SB: public SA { long l4;
6
by: \Frank\ | last post by:
On the Internet there are many descriptions of the memory layout for a DIB. But none that I can find for a Bitmap. Is that because a Bitmap's layout depends on a related device. If that's...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.