According to the previous blog, I think of the memory allocation and management problems of C/C + + and Delphi.

Source: Internet
Author: User

First look at a blog about class creation objects: http://www.cnblogs.com/xumenger/p/4462975.html

Lenovo to allocate memory through pointers

After we have dynamically allocated memory through pointers, we need to remember this pointer (which points to the allocated memory), such as the

int *pi;pi = (int*) malloc (10* sizeof (int));

PI is the address used to record the allocated memory. In Delphi

var    pi:pinteger;begin    Getmem (p, sizeof (Integer) *10); end;

The pi here is also used to record the allocated memory address

As in both cases, if you want PI to point to the new address before releasing the memory, be sure to create a new pointer to hold the value of the Pi first (that is, the address that holds the memory, such as Ptemp)

int* ptemp = pi;//delphi var    ptemp:pinteger;begin ptemp:= Pi;end in C + +    ;

Then let the pi point to the new address, so that it is not to lose control of the memory, so that pi no longer points to the memory, but you can use a new pointer to the memory of the memory to release the RAM, not the memory leaks, such as in C + + and Delphi

Free (ptemp),//delphi Freemem (ptemp);

  

Why do you remember the address of this memory?

Because you need to allocate this memory to use this memory to store data, manipulate the data here, and eventually release the memory.

Only you record the address of this memory, you can use this memory through pointers, and finally can release the memory, otherwise the memory of the horse lost the memory of the address, on the one hand cannot use this memory, on the other hand can not free memory, resulting in memory leaks

According to the previous blog, I think of the memory allocation and management problems of C/C + + and Delphi.

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.