View memory during Delphi 7 debugging

Source: Internet
Author: User

View memory during Delphi 7 debugging

If you are familiar with C language, you can use Delphi programming, especially communication programming, it is often used to directly check the stored value of a piece of memory in the debugging environment to confirm the problem in the code. Viewing memory in Delphi is often not as intuitive as in C. I will summarize the methods used as follows.

If you define a pchar type and use getmem to allocate memory to it, you can check the initial status of the allocated memory in the debugging environment. What should you do?

First, let's look at the following code:

To facilitate debugging, we will remove the optimization options of the Delphi compiler, as shown in:

When executing the following statement in a single step, we check the variable Buf:

Move the mouse cursor over the Buf and we get a bunch of garbled characters,

Use the Debug menu's inspect function to view the Buf variable. We can get the address of the Buf variable: $12f624

Now we can use the memory view function of the Debug menu to view the address $12f624:

We can see that the content saved at this address is $00403277.

In fact, pchar itself is an address type similar to the C language, which stores another address. The address of the newly allocated memory in the following statement will be saved in this variable, after executing the next statement, we will check the value of this address to see what changes have taken place:

With the execution of the getmem statement, the value of the Buf variable changes to a meaningful address: $ 00d53cec. Let's check the current value of this memory, right-click the memory check window on the left and select goto address. In the displayed dialog box, enter $ 00d53cec and press OK. The result is as follows:

Although you have seen countless 0 s, but because we need to allocate 200 bytes, there is a piece of memory in the effective range that contains the string: comctl32.dll ..., this indicates that the getmem function does not initialize the newly allocated memory. In fact, the newly allocated memory may contain any data. As mentioned in the Delphi manual, if allocmem is used here to replace getmem, at this time, all the valid memory values will be 0, because allocmem will initialize the newly allocated memory. If you are interested, try again.

To keep the current window unchanged, press F8 to continue to execute the next statement, and then re-check the data in this memory:

After strcopy is executed, we want the string to appear in the memory. This string is not as long as the string in Delphi. There is an integer in front to save the length, it is a null teminated string that C programmers are familiar with. The string ends with "0, and the part after" 0 "is saved as is.

Continue to run. After the freemem statement is executed, we can see that the previous address is still saved in the Buf variable, and the memory corresponding to this address still keeps the previous content. However, this memory has now been withdrawn by the system and may be reassigned in subsequent getmem or allocmem calls.

Pchar is actually no different from the pointer in C language, but it can be used in combination with Pascal's Char, so it is the most confusing place.

 

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.