473,385 Members | 1,890 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.

C++ Object model layout

The C++ Object Model book says that
'Nonstatic data members are allocated directly within each class
object. Static data members are stored outside the individual class
object. Static and nonstatic function members are also hoisted outside
the class object. Virtual functions are supported in two steps:

A table of pointers to virtual functions is generated for each class
(this is called the virtual table).

A single pointer to the associated virtual table is inserted within
each class object (traditionally, this has been called the vptr). The
setting, resetting, and not setting of the vptr is handled
automatically through code generated within each class constructor,
destructor, and copy assignment operator (this is discussed in Chapter
5). The type_info object associated with each class in support of
runtime type identification (RTTI) is also addressed within the
virtual table, usually within the table's first slot.'

Here I understand that one have some means to access:
- non-static data members and
- virtual function of the class object.

But then how one can access the other entities like:
- static data member
- static and non-static member functions?

How the above entities are represented with respect to class object
layout?
Jun 27 '08 #1
10 2016
sumsin wrote:
The C++ Object Model book says that
'Nonstatic data members are allocated directly within each class
object. Static data members are stored outside the individual class
object. Static and nonstatic function members are also hoisted outside
the class object. Virtual functions are supported in two steps:

A table of pointers to virtual functions is generated for each class
(this is called the virtual table).

A single pointer to the associated virtual table is inserted within
each class object (traditionally, this has been called the vptr). The
setting, resetting, and not setting of the vptr is handled
automatically through code generated within each class constructor,
destructor, and copy assignment operator (this is discussed in Chapter
5). The type_info object associated with each class in support of
runtime type identification (RTTI) is also addressed within the
virtual table, usually within the table's first slot.'
That's a way to do it. I don't think the standard is that specific about how
to implement it. But it is a common aproach yes.
Here I understand that one have some means to access:
- non-static data members and
- virtual function of the class object.

But then how one can access the other entities like:
- static data member
- static and non-static member functions?
Answer this question: how one can access global variables or namespace scope
functions (in C or C++)?

When you answer that you can apply it to your original questions if you
imagine that static data member are just some global variables with
a "special name" (ie struct A { static int a; }; defines a "global" A::a),
static member functions are the same (ie struct A { static void func()
{} }; defines a "global" A::func()) and non static member functions are
similar to static functions only that they have a hidden "this" parameter,
so struct A { void func() {} }; is basically like having struct A { static
void func(A* this) {} };
How the above entities are represented with respect to class object
layout?
They don't have much in common with the object layout.

--
Dizzy

Jun 27 '08 #2
sumsin wrote:
The C++ Object Model book says that
'Nonstatic data members are allocated directly within each class
object. Static data members are stored outside the individual class
object. Static and nonstatic function members are also hoisted outside
the class object. Virtual functions are supported in two steps:

A table of pointers to virtual functions is generated for each class
(this is called the virtual table).

A single pointer to the associated virtual table is inserted within
each class object (traditionally, this has been called the vptr). The
setting, resetting, and not setting of the vptr is handled
automatically through code generated within each class constructor,
destructor, and copy assignment operator (this is discussed in Chapter
5). The type_info object associated with each class in support of
runtime type identification (RTTI) is also addressed within the
virtual table, usually within the table's first slot.'

Here I understand that one have some means to access:
- non-static data members and
- virtual function of the class object.

But then how one can access the other entities like:
- static data member
- static and non-static member functions?
Usually the syntax is <name of the class:: <name of the member>, like

MyClass::myStaticMember

or

MyClass::myStaticFunction(arguments)

And you also can use the 'dot' notation for objects and references and
the 'arrow' notation for pointers to objects if you want to:

MyClass myObject;
MyClass *ptr = &myObject;
... myObject.myStaticMember ...
... ptr->myStaticFunction(arguments) ...
>
How the above entities are represented with respect to class object
layout?
The difference between a regular function and a non-static member
function is that the latter has the hidden argument that designates the
object for which it's being called. Inside the non-static member
function the expression 'this' (a non-modifiable lvalue) gives you the
pointer to the object for which the member function is called. Other
than that, the functions are basically all the same.

Static data members are essentially global data. The only difference is
that class members have access specifiers (which regular global data
don't), but this is "only the compile-time feature", IOW it's not
enforced in any way during run-time (once it compiles, no privacy or
other mechanism exists WRT members compared to non-members).

To quote your own text,
Static data members are stored outside the individual class
object.
That means there is no static data in the class object.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #3
In article <4af52a26-03b6-48f2-b348-82edf5490ba7
@v1g2000pra.googlegroups.com>, su*******@gmail.com says...

[ ... ]
But then how one can access the other entities like:
- static data member
A static data member is just a global variable with a funny name. It's
just assigned an address by the linker, and everything that refers to it
uses that address.
- static and non-static member functions?
Static member functions are just normal global functions with funny
names -- you might have:

class my_class {
public:
static_func() {}
};

and the compiler turns that into a name like:

static_func$my_class$$void

but past the funny name, it's a rather ordinary global function.

A non-static member function is pretty much the same except that the
compiler generates a bit of extra code so that the function receives a
pointer to the object on which it's being invoked (what's referred to as
'this' in the source code). This is sufficiently common that some
compilers do things like reserving a register specifically for holding
'this'.
How the above entities are represented with respect to class object
layout?
They're all just globals with funny names, so there's essentially no
relationship between them and the class layout at all.

Of course, I'm only talking about the typical implementation -- in
theory, all of this _could_ undoubtedly be done in other ways.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #4
On Jun 11, 8:09 pm, Jerry Coffin <jcof...@taeus.comwrote:
In article <4af52a26-03b6-48f2-b348-82edf5490ba7
@v1g2000pra.googlegroups.com>, sumsin...@gmail.com says...

[ ... ]
But then how one can access the other entities like:
- static data member

A static data member is just a global variable with a funny name. It's
just assigned an address by the linker, and everything that refers to it
uses that address.
- static and non-static member functions?

Static member functions are just normal global functions with funny
names -- you might have:

class my_class {
public:
static_func() {}

};

and the compiler turns that into a name like:

static_func$my_class$$void

but past the funny name, it's a rather ordinary global function.

A non-static member function is pretty much the same except that the
compiler generates a bit of extra code so that the function receives a
pointer to the object on which it's being invoked (what's referred to as
'this' in the source code). This is sufficiently common that some
compilers do things like reserving a register specifically for holding
'this'.
How the above entities are represented with respect to class object
layout?

They're all just globals with funny names, so there's essentially no
relationship between them and the class layout at all.

Of course, I'm only talking about the typical implementation -- in
theory, all of this _could_ undoubtedly be done in other ways.

--
Later,
Jerry.

The universe is a figment of its own imagination.
You mean static data members are just like global variable and non-
static and static member functions are just like normal function. So
these entities do not have any concern with the class object. Is it?
Jun 27 '08 #5
sumsin wrote:
You mean static data members are just like global variable and non-
static and static member functions are just like normal function. So
these entities do not have any concern with the class object. Is it?
Exactly. This way it will help you later to understand how pointer to
members (especially to member functions) work. Many people don't understand
these basic things you asked about and thus they come asking why giving a
member function pointer value where a function pointer is expected does not
work.

You can play doing this, struct A {}; Print its sizeof(). Then add a static
function. Print again its sizeof. Then add a member function and print
again the sizeof(). Then add a static data member and print the sizeof.

--
Dizzy

Jun 27 '08 #6
On Jun 11, 8:45 pm, dizzy <di...@roedu.netwrote:
sumsin wrote:
You mean static data members are just like global variable and non-
static and static member functions are just like normal function. So
these entities do not have any concern with the class object. Is it?

Exactly. This way it will help you later to understand how pointer to
members (especially to member functions) work. Many people don't understand
these basic things you asked about and thus they come asking why giving a
member function pointer value where a function pointer is expected does not
work.

You can play doing this, struct A {}; Print its sizeof(). Then add a static
function. Print again its sizeof. Then add a member function and print
again the sizeof(). Then add a static data member and print the sizeof.

--
Dizzy
Great! I really enjoyed the play. Also when I add a virtual function
the size increases by 4 byte and that because 'vptr' right!
Jun 27 '08 #7
On Jun 11, 8:09 pm, Jerry Coffin <jcof...@taeus.comwrote:
In article <4af52a26-03b6-48f2-b348-82edf5490ba7
@v1g2000pra.googlegroups.com>, sumsin...@gmail.com says...

[ ... ]
But then how one can access the other entities like:
- static data member

A static data member is just a global variable with a funny name. It's
just assigned an address by the linker, and everything that refers to it
uses that address.
- static and non-static member functions?

Static member functions are just normal global functions with funny
names -- you might have:

class my_class {
public:
static_func() {}

};

and the compiler turns that into a name like:

static_func$my_class$$void

but past the funny name, it's a rather ordinary global function.

A non-static member function is pretty much the same except that the
compiler generates a bit of extra code so that the function receives a
pointer to the object on which it's being invoked (what's referred to as
'this' in the source code). This is sufficiently common that some
compilers do things like reserving a register specifically for holding
'this'.
How the above entities are represented with respect to class object
layout?

They're all just globals with funny names, so there's essentially no
relationship between them and the class layout at all.
So how they are maintained?
I think compiler create a list with these funny names and linker
assign their corresponding addresses, when ever anybody approach any
funny name that is dereference to the corresponding memory address,
right?
>
Of course, I'm only talking about the typical implementation -- in
theory, all of this _could_ undoubtedly be done in other ways.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #8
In article <8a1ec585-3d56-44cf-9b41-1dfb9558e848
@c19g2000prf.googlegroups.com>, su*******@gmail.com says...

[ ... ]
So how they are maintained?
I think compiler create a list with these funny names and linker
assign their corresponding addresses, when ever anybody approach any
funny name that is dereference to the corresponding memory address,
right?
Yes, that's pretty much correct. Of course, there can be some exceptions
-- for example, one of the reasons separate compilation of templates is
so difficult to implement is that you can instantiate a template over a
new type. The meaning of code in the template may change, depending on
the type over which it's instantiated, so instantiating over a new type
can mean the template needs to be re-compiled completely, generating
completely different code.

Since the exported template was compiled separately, however, this
requirement for recompilation isn't visible until link time. The linker
checks whether the object file for the template contains code for the
type(s) over which that template is instantiated, and forces it to be
compiled over the new types if needed. After that the link proceeds as
normal.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #9
On Jun 11, 5:32 pm, sumsin <sumsin...@gmail.comwrote:
On Jun 11, 8:09 pm, Jerry Coffin <jcof...@taeus.comwrote:
In article <4af52a26-03b6-48f2-b348-82edf5490ba7
@v1g2000pra.googlegroups.com>, sumsin...@gmail.com says...
[ ... ]
But then how one can access the other entities like:
- static data member
A static data member is just a global variable with a funny
name. It's just assigned an address by the linker, and
everything that refers to it uses that address.
- static and non-static member functions?
Static member functions are just normal global functions
with funny names -- you might have:
class my_class {
public:
static_func() {}
};
and the compiler turns that into a name like:
static_func$my_class$$void
but past the funny name, it's a rather ordinary global
function.
A non-static member function is pretty much the same except
that the compiler generates a bit of extra code so that the
function receives a pointer to the object on which it's
being invoked (what's referred to as 'this' in the source
code). This is sufficiently common that some compilers do
things like reserving a register specifically for holding
'this'.
How the above entities are represented with respect to
class object layout?
They're all just globals with funny names, so there's
essentially no relationship between them and the class
layout at all.
Of course, I'm only talking about the typical implementation
-- in theory, all of this _could_ undoubtedly be done in
other ways.
I was waiting for that disclaimer:-). (It's another difference
between theory and practice: in theory, it could be done in
another way; in practice, it's not.)
You mean static data members are just like global variable and
non- static and static member functions are just like normal
function. So these entities do not have any concern with the
class object. Is it?
With regards to generated code, yes. Static members are still
members, however, and things like access control are applied.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 27 '08 #10
On Jun 11, 10:14*am, sumsin <sumsin...@gmail.comwrote:
The C++ Object Model book says that
'Nonstatic data members are allocated directly within each class
object. Static data members are stored outside the individual class
object. Static and nonstatic function members are also hoisted outside
the class object. Virtual functions are supported in two steps:

A table of pointers to virtual functions is generated for each class
(this is called the virtual table).

A single pointer to the associated virtual table is inserted within
each class object (traditionally, this has been called the vptr). The
setting, resetting, and not setting of the vptr is handled
automatically through code generated within each class constructor,
destructor, and copy assignment operator (this is discussed in Chapter
5). The type_info object associated with each class in support of
runtime type identification (RTTI) is also addressed within the
virtual table, usually within the table's first slot.'

Here I understand that one have some means to access:
- non-static data members and
- virtual function of the class object.

But then how one can access the other entities like:
- static data member
- static and non-static member functions?

How the above entities are represented with respect to class object
layout?
The following articles should help:
http://www.eventhelix.com/realtimema...erformance.htm
http://www.eventhelix.com/realtimema...formance2..htm

--
http://www.EventHelix.com/EventStudio
Sequence diagram based systems engineering tool
Jun 27 '08 #11

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

Similar topics

13
by: Droolboy | last post by:
I'm trying to build a fairly small (max. 10 different pages) site using php, and it's becoming obvious that I need some kind of model view separation. Having done a few searches, I've come...
21
by: Jure Sah | last post by:
Hello, I have been promising an object orientated ASM IDE for a while now. Trying to make the best of what is already here, I have made a compiler, that will parse XML code and output ASM files....
13
by: Stephen Poley | last post by:
Good advice is frequently given in c.i.w.a.* on page structure: matters such as separation of content from presentation, graceful degradation, non-dependence on Flash etc. For some while I've...
8
by: al.cpwn | last post by:
Which sections of the 2003 standard should I study to understand the object model? I am looking to find information similar to that in Stanley B. Lippman's book, "Inside the C++ Object Model"
50
by: Shadow Lynx | last post by:
Consider this simple HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head>...
3
by: mailursubbu | last post by:
Hi, Can you please refer me the book (or link) which I should refer to for memory layout of a object in c++. How the virtual table is used and other such details i am interested to know. If...
1
realin
by: realin | last post by:
hello guys, I am just unable to make a simple 2 Box model layout layout work perfect on the browsers IE and ffx. My resulting webpage responds variably on either of them.. The problem is that...
14
by: sumsin | last post by:
From 'Inside the C++ Object Model' by 'Stanley B. Lippman' 'The primary strength of the C++ Object Model is its space and runtime efficiency. Its primary drawback is the need to recompile...
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.