Problems with dynamic memory allocation (memory hole)------C + + programming principles and Practices (advanced)

Source: Internet
Author: User

Where is the question of new? In fact, the problem is with the combination of new and delete. Examine the memory allocation and release process in the following program:

while (1) {    big* p=new Big;
//...... small* n1=new Small;
//...... Delete p; small* n2=new Small;
//......}

In each loop step, we created two small, during which we also allocated a big and then released it. Looking at this code, we may expect to "consume" 2*sizeof (Small) bytes of memory (plus the additional overhead of dynamic memory allocation) each time we perform a looping step. In practice, however, each cycle step consumes more memory.

As you can see, there is some unused space ("holes") left in dynamic memory for Each loop step. These voids can be just a few bytes in size, but if we can't use them effectively, the damage is the same as a memory leak. In memory, the free space is dispersed, creating a lot of small "holes" that cannot meet new memory requirements, called memory fragmentation. The memory management program will eventually run out of "holes" that are large enough to leave only small holes that cannot be used. This is a serious problem that can occur with any system that uses new and delete frequently after a long run.

Problems with dynamic memory allocation (memory hole)------C + + programming principles and Practices (advanced)

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.