Inside the C++ Object Model

Category: Programming
Author: Stanley B. Lippman
4.7
All Stack Overflow 23
This Year Stack Overflow 1
This Month Stack Overflow 9

Comments

by anonymous   2019-07-21

What is generally referred to as memory model refers to the interaction of different threads of execution accessing objects. In this sense it is entirely tied to concurrency. It isn’t reall about memory.

There is another important aspect which is referred to as object model and Stan Lippman’s book is an excellent resource for learning about that (it is a bit dated but stays mostly relevant). The object model also doesn’t really talk about how memory is accessed.

The closest to a description of how memory is accessed in general is Ulrich Drepper’s What Every Programmer Should Know About Memory_. This artivle is about the general view of memory, independent of programming languages. Of course, depending on the object model different programming languages may hide direct interaction with memory.

by anonymous   2019-07-21

You either misunderstand sizeof or your misunderstand the layout (in memory) of C++ objects.

For performance reason (to avoid the cost of indirection), compilers will often implement Derivation using Composition:

// A
+---+
| i |
+---+

// B
+---+---+
| A | j |
+---+---+

Note that if private, B cannot peek in A even though it contains it.

The sizeof operator will then return the size of B, including the necessary padding (for alignment correction) if any.

If you want to learn more, I heartily recommend Inside the C++ Object Model by Stanley A. Lippman. While compiler dependent, many compilers do in fact use the same basic principles.

by anonymous   2019-07-21

Personally I like this one. Not a compiler's eye view exactly, but it tells you what's going on "under the hood" of a C++ program.

Inside the C++ Object Model

by anonymous   2017-12-11
Here is a good book to learn more about object model: https://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545
by litb   2017-08-20

Beginner

Introductory, no previous programming experience

Introductory, with previous programming experience

* Not to be confused with C++ Primer Plus (Stephen Prata), with a significantly less favorable review.

Best practices


Intermediate


Advanced


Reference Style - All Levels

C++11/14 References:

  • The C++ Standard (INCITS/ISO/IEC 14882-2011) This, of course, is the final arbiter of all that is or isn't C++. Be aware, however, that it is intended purely as a reference for experienced users willing to devote considerable time and effort to its understanding. As usual, the first release was quite expensive ($300+ US), but it has now been released in electronic form for $60US.

  • The C++14 standard is available, but seemingly not in an economical form – directly from the ISO it costs 198 Swiss Francs (about $200 US). For most people, the final draft before standardization is more than adequate (and free). Many will prefer an even newer draft, documenting new features that are likely to be included in C++17.

  • Overview of the New C++ (C++11/14) (PDF only) (Scott Meyers) (updated for C++1y/C++14) These are the presentation materials (slides and some lecture notes) of a three-day training course offered by Scott Meyers, who's a highly respected author on C++. Even though the list of items is short, the quality is high.

  • The C++ Core Guidelines (C++11/14/17/…) (edited by Bjarne Stroustrup and Herb Sutter) is an evolving online document consisting of a set of guidelines for using modern C++ well. The guidelines are focused on relatively higher-level issues, such as interfaces, resource management, memory management and concurrency affecting application architecture and library design. The project was announced at CppCon'15 by Bjarne Stroustrup and others and welcomes contributions from the community. Most guidelines are supplemented with a rationale and examples as well as discussions of possible tool support. Many rules are designed specifically to be automatically checkable by static analysis tools.

  • The C++ Super-FAQ (Marshall Cline, Bjarne Stroustrup and others) is an effort by the Standard C++ Foundation to unify the C++ FAQs previously maintained individually by Marshall Cline and Bjarne Stroustrup and also incorporating new contributions. The items mostly address issues at an intermediate level and are often written with a humorous tone. Not all items might be fully up to date with the latest edition of the C++ standard yet.

  • cppreference.com (C++03/11/14/17/…) (initiated by Nate Kohl) is a wiki that summarizes the basic core-language features and has extensive documentation of the C++ standard library. The documentation is very precise but is easier to read than the official standard document and provides better navigation due to its wiki nature. The project documents all versions of the C++ standard and the site allows filtering the display for a specific version. The project was presented by Nate Kohl at CppCon'14.


Classics / Older

Note: Some information contained within these books may not be up-to-date or no longer considered best practice.

by anonymous   2017-08-20

Considering a quite popular post here on SO , there are lot of solutions to your problem, probably the best one is this book.

I would like to also recommend another path: pick 1 between Obj-C and Java if you can deviate a little from the original focus.

The reason why I'm suggesting this is the long time existence of Java, combined with its big popularity in both production and education, and the fact that Obj-C is somehow an object oriented language that "exposed" itself in the way this mechanisms works, there are a lot of pointers and really simple but powerful concepts that can help you understand this.

It's also possible to use Obj-C on platforms that are not MAC OS driven but you should use clang, not gcc, gcc is a little bit behind on objc support, at least this is what I experienced.

There is also the usual list of free resources with a lot of goodies that always helps .

by anonymous   2017-08-20

Apart from the accessibility of members outside or to the derived classes, access specifiers might affect the object layout.

Quoting from my other answer:

Usually, memory address for data members increases in the order they're defined in the class . But this order may be disrupted at any place where the access-specifiers (private, protected, public) are encountered. This has been discussed in great detail in Inside the C++ Object Model by Lippman.

An excerpt from C/C++ Users Journal,

The compiler isn't allowed to do this rearrangement itself, though. The standard requires that all data that's in the same public:, protected:, or private: must be laid out in that order by the compiler. If you intersperse your data with access specifiers, though, the compiler is allowed to rearrange the access-specifier-delimited blocks of data to improve the layout, which is why some people like putting an access specifier in front of every data member.

Interesting, isn't it?

by Mike B   2017-08-20

Chris Jester-Young gives the basic answer to this question.

Wikipedia has a more in depth treatment.

If you want to know the full details for how this type of thing works (and for all type of inheritance, including multiple and virtual inheritance), one of the best resources is Stan Lippman's "Inside the C++ Object Model".

by anonymous   2017-08-20

When we declare object of a class is its memory allocation successive(One after the other)?

The Standard doesn't give any such guarantee. Object memory layout is implementation-defined.

Usually, memory address for data members increases in the order they're defined in the class . But this order may be disrupted at any place where the access-specifiers (private, protected, public) are encountered. This has been discussed in great detail in Inside the C++ Object Model by Lippman.

An excerpt from C/C++ Users Journal,

The compiler isn't allowed to do this rearrangement itself, though. The standard requires that all data that's in the same public:, protected:, or private: must be laid out in that order by the compiler. If you intersperse your data with access specifiers, though, the compiler is allowed to rearrange the access-specifier-delimited blocks of data to improve the layout, which is why some people like putting an access specifier in front of every data member.

Interesting, isn't it?

by anonymous   2017-08-20

When you create an object of the derived class, a base class sub-object is embedded in the memory layout of the derived class object. So, to your question, there's only on variable that will be a part of the derived object. Since, we are only taking about non-static members here, each derived object gets its base-class sub-object laid out in memory. When you create a base class object, its a different piece of memory representing different object and has nothing to do with derived object created earlier.

Hope it clarifies your doubt!

This is a great book to understand C++ object model:

http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545/ref=sr_1_1?ie=UTF8&qid=1412535828&sr=8-1&keywords=inside+c%2B%2B+object+model

by anonymous   2017-08-20

The C++ standard does not prescribe how the virtual function mechanism should be implemented. In practice all C++ implementations use a virtual function table per class, and a virtual function table pointer in each object of class with virtual functions (called a polymorphic class). Yet the details can differ, in particular for multiple inheritance and virtual inheritance.

You can read about the common choices in Stanley Lippman's classic book Inside The C++ Object Model.

It doesn't make much sense to ask “where” a virtual function table is stored. It's much like any static variable: its location depends on the implementation and is pretty much arbitrary. And regarding

Why not near with the classes?

… classes as such are not stored anywhere, they are not objects, so this doesn't make sense, sorry.

You can ask more meaningfully where is the vtable pointer stored in each object, for a given implementation?

And usually that's at the start of the object, but if you derive from a non-polymorphic class, and add a virtual function, then you might get the vtable pointer somewhere else. Or not. The latter possibility is much of the reason why a static_cast of Derived* to Base* (or vice versa) can do an address adjustment, i.e. is different from a simple reinterpret_cast.