C + + Object memory model __c++

Source: Internet
Author: User
Tags exception handling

on the object model of VC + +
(US) Jane Gray
Chenghua translation

Preface of the Translator

A C + + programmer who wants to improve the level of technology should learn more about the semantic details of some languages. For the use of VC + + programmers, you should also know some VC + + for the interpretation of C + +. Inside the C + + Object model Although is a good book, however, the book more space, and the specific VC + + relationship smaller. Therefore, in terms of space and content, the translator thinks this article is a good starting point to understand the C + + object model in depth.
This article was seen before the very good, the old text reread, feel more understanding, so produced a translation, and share the idea. Although the article is not long, but time is limited, and several times in the translation nap asleep, procrastination spent one months.
On the one hand because my level is limited, on the other hand because the translation often nap, the wrong place is afraid many, welcome everybody criticize correct.

The original source of this article is MSDN. If you install MSDN, you can search for C + + Under the Hood. Otherwise, you can find http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarvc/html/jangrayhood.asp on the website.

1 Preface

Knowing how your programming language is implemented can be particularly meaningful for C + + programmers. First of all, it removes the mystery of the language we use, so that we are not completely incredulous about what the compiler does, and, above all, it gives us more certainty when we debug and use the advanced features of the language. This knowledge can also help us well when it comes to improving the efficiency of the Code.

This article focuses on answering some of these questions:
How the 1* class is laid out.
2* How member variables are accessed.
3* how member functions are accessed.
4* so-called "adjustment block" (adjuster thunk) is going on.
5* the cost of using the following mechanism:
* Single inheritance, multiple inheritance, virtual inheritance
* Virtual function call
* Cast to base class, or cast to virtual base class
* Exception Handling
First, we examine the layout of C-compatible structures (struct), single inheritance, multiple inheritance, and virtual inheritance.
Next, we talk about the access of member variables and member functions, of course, where the bread contains virtual functions;
Next, we examine how constructors, destructors, and special assignment operator member functions work, and how the arrays are constructed and destroyed dynamically;
Finally, the support for exception handling is simply introduced.

For each language feature, we will briefly describe the motives behind the feature, its own semantics (of course, this article is not "Introduction to C + +", we should be fully aware of this), and this feature in Microsoft's VC + + is how to achieve. Note here to distinguish between abstract C + + language semantics and their specific implementations. Other C + + vendors outside of Microsoft may provide a completely different implementation, and we occasionally compare the implementation of VC + + with other implementations.

2 class Layout

This section discusses the different memory layouts that are caused by different inheritance patterns.

2.1 c structure (struct)

C + + is also "basically" compatible with C, because it is based on C. In particular, the C + + specification uses the same as C on the structure, the simple single memory layout principle: The member variables are arranged in the order in which they are declared, aligned on the memory address according to the alignment principle specified in the implementation. all of the C + + vendors guarantee that their C/C + + compiler will have the exact same layout for the effective structure of the structures. Here, A is a simple C structure whose member layout and alignment are at a glance

View plain Copy to clipboard print?      struct A {char c;   int i; };

Translator Note: From the figure above, a in memory occupies 8 bytes, according to the order of the declared members, the first 4 bytes contain a character (actually occupy 1 bytes, 3 bytes empty, complement), after 4 bytes contains an integer. A's pointer points to the byte at which the character begins.

2.2 C-structure with C + + features

Of course, C + + is not a complex c,c++ is essentially an object-oriented language: Contains inheritance, encapsulation, and polymorphism. The original C structure has been transformed into the cornerstone of the object-oriented world-class. In addition to member variables, C + + classes can encapsulate member functions and other things. Interestingly, however, unless, in order to implement hidden member variables introduced by virtual functions and virtual inheritance, the size of the C + + class instance depends entirely on the member variables of a class and its base class. The member function basically does not affect the size of the class instance.

The b provided here is a C structure, however, the structure has some C + + features: the "public/protected/private" keyword that controls member visibility, member functions, static members, and nested type declarations. While looking at the dazzling, in fact, only member variables occupy the space of class instances . It is important to note that the C + + Standard committee does not limit the order in which the segments separated by the "public/protected/private" keyword are implemented, so the memory layouts implemented by different compilers may not be the same. (in VC + +, member variables are always listed in the order in which they are declared).

View plain Copy to clipboard print?   struct B {public:int bm1;   Protected:int bm2;      Private:int bm3;      static int BSM;      void Bf ();      static void BSF ();      typedef void * BPV;   struct N {}; };

Translator Note: In B, why the static int BSM does not occupy memory space. Because it is a static member, the data is stored in the data section of the program , not in the class instance.

2.3 Single Inheritance

C + + provides inheritance for the purpose of extracting commonalities between different types. For example, scientists classify species, thus having a kind of, genus, outline, etc. With this hierarchy, it is possible to classify something of a certain nature into the most appropriate classification level, such as "a mammal with a child". Since these attributes can be inherited by the quilt class, we can easily point out that "whales and humans can conceive children" simply by knowing that "whales and humans" are mammals. Those exceptions, such as the Platypus (egg-laying mammals), require us to overwrite the default attributes or behaviors.
The inheritance syntax in C + + is simple, adding ": Base" to the subclass. The following d inherits from base class C.

View plain Copy to clipboard print?      struct C {int C1;   void CF (); };

View plain Copy to clipboard print?      struct D:C {int d1;   void DF (); };

Since the derived class retains all the properties and behavior of the base class, naturally, an instance of each derived class contains a complete base class instance data. In D, it is not that the base class C data must be placed in the data of D, but in this way, the C object address in D is guaranteed to be the first byte of the D object address. Under this arrangement, with pointers to derived class D, to get a pointer to base class C, you do not have to compute

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.