Delphi-reasons for consistency

Source: Internet
Author: User

Hello everyone, I'm huangjacky, technical exchange.
I mentioned a problem in the previous article and later found that the problem was there. However, it was too late to explain the cause carefully. now let's talk about the reason.
If you have time, read my other article first.ArticleDelphi-object structure analysis.

First, let's look at the memory space of object:

Let's take 4 bytes as a unit to see what each means.
First, check the address $ 004bd47c:

From the character next to it, we can see that this is the definition of tderive.
Now let's see what $00401c94 is. Let's ignore the four zeros in the middle.

There are three addresses. Now we areCodeIn the editor, let's see what code these three addresses are.

These three addresses are originally the addresses of the iinterface implementation methods in tderive. because tderive is inherited from tbase, while tbase is inherited from tinterfacedobject, three methods of iinterface are not modified throughout the inheritance process, therefore, we can see that the above image is directly redirected to tinterfacedobject.
Next let's take a look at the content on $004bd2d0.

There are four suspicious addresses, which we can also view in the Code Editor:

No. This is the four methods corresponding to the iBase interface. Do you have to ask if the iBase interface has only one talkbase method? But do not forget that IBASE inherits the iinterface by default. SO 3 + 1 = 4.
Next $00000037 = 55; this is the memory area of ftest, an integer.
Next let's take a look at the content of $ 004bd3ec:


Five IP addresses are suspicious. A wise friend must have guessed that this is the pointer list of the iderive interface implementation method. 1 + 1 + 3 = 5. But let's take a look:

Okay, We guessed it was correct. Okay, we can divide it now.

We can see that the entire data arrangement process is the same as the inheritance process.Display class name-> inherit data fields from tinterfacedobject (here only frefcount: integer)-> implement iinterface method-> inherit data fields from tbase (no field defined) -> methods for implementing the iBase interface-> data fields defined by tderive-> methods for implementing the iderive interface-> end with $00 00 00.

With the above conclusion, we can know that the compiler goes to. in the ftest data process, first take the pointing address of a, that is, the starting position of the actual memory of object, then, an offset of 16 bytes is used to take a pinteger ^ as the value of ftest.
Next, let's see what the Add address is.

That is, the iBase I drew with the Red Arrow. When we use an incorrect image

1:A: = tderive (flist [0]);

2:Showmessage (inttostr (A. ftest ));

It will appear at the address of $ ad2a2c offset of 16 bytes. Let's see what this location is on the memory?

Yes $ ad2680 = 11347584. Let's see if there are so many.

Haha, it is the same as what I am looking.

The correct method

1:A: = tderive (IBASE (flist [0]);

Tderive (IBASE here calculates the starting offset of iBase and tderive =-12 bytes.

The problem is clearly explained. The pointer is offset when we store the pointer, so we need to reverse offset the corresponding address before using it.

Thank you! I'm huangjacky. I have washed my feet.

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.