High-quality C++/C Programming Guide-7th Chapter-Memory Management (5)

Source: Internet
Author: User

7.9 memory exhaustion How to do?
If a large enough chunk of memory is not found when requesting dynamic memory, malloc and new return a null pointer, declaring that the memory request failed. There are usually three ways to handle the "memory exhaustion" problem.

(1) Determine if the pointer is null, and if so, terminate the function with the return statement immediately. For example:

void Func (void)

{

A *a = new A;

if (a = = NULL)

{

Return

}

...

}

(2) To determine whether the pointer is null, if it is immediately with exit (1) to terminate the operation of the entire program. For example:

void Func (void)

{

A *a = new A;

if (a = = NULL)

{

cout << "Memory exhausted" << Endl;

Exit (1);

}

...

}

(3) Set the exception handler function for new and malloc. For example, Visual C + + can use the _set_new_hander function to set the user-defined exception handling function for new, or let malloc enjoy the same exception handling function as new. For more information, please refer to C + + usage manual.

The above (1) (2) mode is most commonly used. If there are multiple places in a function to request dynamic memory, then the way (1) appears to be powerless (freeing memory is cumbersome), should be used in the way (2) to deal with.

Many people do not have the heart to use Exit (1), asked: "Do not write error handling procedures, so that the operating system to solve their own line?" ”

No way. If something like "memory exhaustion" occurs, the application is generally no longer available. If you do not use Exit (1) to kill the bad program, it may kill the operating system. The truth is like this: if you don't shoot the gangsters, the gangsters will commit more sins before they die.

There is a very important phenomenon to tell everyone. For more than 32 applications, no matter how malloc and new are used, it is almost impossible to cause "memory exhaustion." I wrote a test program in Visual C + + under Windows 98, as shown in example 7-9. This program will run indefinitely and will not terminate at all. Because the 32-bit operating system support "virtual", memory used up, automatically use hard disk space replacement. I only heard the hard drive crunch, Window 98 is tired of the keyboard, mouse unresponsive.

I can conclude that the "memory exhaustion" error handler is useless for more than 32-bit applications. This can make UNIX and Windows programmers happy: Anyway, the error handler does not work, I will not write, save a lot of trouble.

I do not want to mislead the reader, must emphasize: no error handling will result in poor quality of the program, should not be wise and foolish.

void Main (void)

{

float *p = NULL;

while (TRUE)

{

p = new float[1000000];

cout << "Eat memory" << Endl;

if (p==null)

Exit (1);

}

}

Example 7-9 trying to run out of memory on the operating system

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.