Memory layout when C ++ is running ---- what is this?

Source: Internet
Author: User

Address: http://blog.csdn.net/smstong/article/details/6604388

 

First, let's ask a question. In C ++, is the this pointer in the member function always the same as the address of the object that calls this function? If your answer is: Not necessarily. At least you are a veteran. You don't need to read the following content. If your answer is yes, you are strongly advised to take a look at the following content.

 

Non-static member functions, whether virtual or not, hide a this pointer parameter. The purpose of this parameter is to provide a base address for the function so that the function can find the member variables of the object. Then how does a non-static member function find member variables based on the this pointer? Let's look at the example.

 

1. No virtual table

  1. # Include <iostream>
  2. # Include <stdio. h>
  3. Using namespace STD;
  4. Class
  5. {
  6. Public:
  7. Int X;
  8. Int y;
  9. Public:
  10. Void F1 ()
  11. {
  12. This-> x = 1;
  13. This-> Y = 2;
  14. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  15. }
  16. };
  17. Int main (INT argc, char ** argv)
  18. {
  19. A;
  20. Cout <"the address of object A is:" <& A <Endl;
  21. Cout <"the size of object A is:" <sizeof (a) <Endl;
  22. Cout <"the address of a. x member is:" <& A. x <Endl;
  23. Cout <"the offset of A. X Member is:" <& A: x <Endl;
  24. A. F1 ();
  25. Cin> argc;
  26. Return 0;
  27. }

Then the pseudo code of function F1 is:

 

* (This + & A: x-1) = 1;

* (This + & A: Y-1) = 2;

 

Where & A: X is the offset of member x + 1, & A: Y is the offset of member y + 1, which is the basic syntax knowledge of C ++, hope you know. But these offsets correspond to the corresponding offsets. Is it the object address? The answer is no. It is the offset relative to the first member variable, which may not be different for some objects, but for objects with classes with virtual tables, there is a difference. These offsets are determined during compilation. In this example, & A: X under VC ++ 2010 has a value of 1, & A: Y, and a value of 5. For more information about the reason, see inside the C ++ object model.

Therefore, only the value of this needs to be determined for finding the member variable. The program running result is as follows:

In this example, the object address is the same as the address of this pointer. The memory figure is as follows.

2. There is a virtual table.

  1. # Include <iostream>
  2. # Include <stdio. h>
  3. Using namespace STD;
  4. Class
  5. {
  6. Public:
  7. Int X;
  8. Int y;
  9. Public:
  10. Virtual void F1 ()
  11. {
  12. This-> x = 1;
  13. This-> Y = 2;
  14. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  15. }
  16. };
  17. Int main (INT argc, char ** argv)
  18. {
  19. A * P = new ();
  20. Cout <"the address of object A is:" <p <Endl;
  21. Cout <"the size of object A is:" <sizeof (a) <Endl;
  22. Cout <"the address of a. x member is:" <& P-> x <Endl;
  23. Cout <"the offset of A. X Member is:" <& A: x <Endl;
  24. P-> F1 ();
  25. Cin> argc;
  26. Return 0;
  27. }

 

In this case, the pseudo code of function F1 is:

 

* (This + 4 + & A: x-1) = 1; // + 4 because there is a virtual table pointer

* (This + 4 + & A: Y-1) = 2; // + 4 because there is a virtual table pointer

 

The program running result is as follows:

The memory layout is as follows:

Conclusion: The value of this is the same as the object address, and the offset of the member variable does not change because of the existence of the virtual table pointer. The member functions of classes with virtual tables have different addressing methods for member variables. Add one + 4.

3. Single inheritance

  1. # Include <iostream>
  2. # Include <stdio. h>
  3. Using namespace STD;
  4. Class
  5. {
  6. Public:
  7. Int X;
  8. Int y;
  9. Public:
  10. Void F1 ()
  11. {
  12. This-> x = 1;
  13. This-> Y = 2;
  14. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  15. }
  16. };
  17. Class B: public
  18. {
  19. Public:
  20. Int Z;
  21. Public:
  22. Virtual void F2 ()
  23. {
  24. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  25. }
  26. };
  27. Int main (INT argc, char ** argv)
  28. {
  29. B * pb = new B ();
  30. Cout <"the object address is:" <STD: Hex <STD: showbase <Pb <Endl;
  31. Pb-> F1 ();
  32. Pb-> F2 ();
  33. Cin> argc;
  34. Return 0;
  35. }

 

Running result:

Memory layout:

Conclusion: The value of this pointer is determined by two factors: one is the object address, and the other is the class that defines the member function. This points to the subobject class that defines the method in the object.

4 Multi-Inheritance

First, check whether a has a virtual function or B has a virtual function.

  1. # Include <iostream>
  2. # Include <stdio. h>
  3. Using namespace STD;
  4. Class
  5. {
  6. Public:
  7. Int X;
  8. Int y;
  9. Public:
  10. Void F1 ()
  11. {
  12. This-> x = 1;
  13. This-> Y = 2;
  14. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  15. }
  16. };
  17. Class B
  18. {
  19. Public:
  20. Int Z;
  21. Public:
  22. Virtual void F2 ()
  23. {
  24. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  25. }
  26. };
  27. Class C: Public A, public B
  28. {
  29. Public:
  30. Int;
  31. Public:
  32. Virtual void F2 ()
  33. {
  34. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  35. }
  36. Void F3 ()
  37. {
  38. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  39. }
  40. };
  41. Int main (INT argc, char ** argv)
  42. {
  43. C * Pc = new C ();
  44. Cout <"object size:" <sizeof (c) <Endl;
  45. Cout <"the object address is:" <STD: Hex <STD: showbase <Pc <Endl;
  46. PC-> F1 ();
  47. PC-> F2 ();
  48. PC-> F3 ();
  49. Cin> argc;
  50. Return 0;
  51. }

Result:

Memory layout:

Again, if a and B both have virtual functions.

Code:

  1. # Include <iostream>
  2. # Include <stdio. h>
  3. Using namespace STD;
  4. Class
  5. {
  6. Public:
  7. Int X;
  8. Int y;
  9. Public:
  10. Virtual void F1 ()
  11. {
  12. This-> x = 1;
  13. This-> Y = 2;
  14. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  15. }
  16. };
  17. Class B
  18. {
  19. Public:
  20. Int Z;
  21. Public:
  22. Virtual void F2 ()
  23. {
  24. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  25. }
  26. };
  27. Class C: Public A, public B
  28. {
  29. Public:
  30. Int;
  31. Public:
  32. Virtual void F2 ()
  33. {
  34. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  35. }
  36. Void F3 ()
  37. {
  38. Cout <"the value of this pointer is:" <STD: Hex <STD: showbase <This <Endl;
  39. }
  40. };
  41. Int main (INT argc, char ** argv)
  42. {
  43. C * Pc = new C ();
  44. Cout <"object size:" <sizeof (c) <Endl;
  45. Cout <"the object address is:" <STD: Hex <STD: showbase <Pc <Endl;
  46. Cout <"X address" <& PC-> x <Endl;
  47. Cout <"Z address" <& PC-> Z <Endl;
  48. PC-> F1 ();
  49. PC-> F2 ();
  50. PC-> B: F2 ();
  51. PC-> F3 ();
  52. Cin> argc;
  53. Return 0;
  54. }

Result:

Memory layout:

Conclusion: The method for determining the value of this pointer is verified again. This must always point to the subobject class that defines the member function. C ++ ensures that the base class subobject exactly corresponds to the base class object, so that the member function can address the offset of the variable in the class defined by the member variable.

 

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.