C + +: memory leaks and the concept of wild pointers

Source: Internet
Author: User

Memory leaks

The space dynamically opened up with the dynamic storage allocation function is not released after use, resulting in the memory element being occupied. Until the end of the program. This is called a memory leak.

Note: A memory leak refers to a leak in heap memory.

Simple is to apply for a piece of memory space, the use is not released after completion. The general way it behaves is that the longer the program runs, the more memory is consumed, and eventually all the memory is exhausted, and the entire system crashes. A piece of memory requested by the program, and no pointer to it, then this memory is leaked.

Wild Hands

The "Wild pointer" is not a null pointer, it is a pointer to "junk" memory. It is generally not wrong to use a null pointer because it is easy to judge with an if statement. But a "wild pointer" is dangerous, and the IF statement does not work on it. There are two main causes of wild pointers:

One, the pointer variable is not initialized. Any pointer variable that has just been created does not automatically become a null pointer, and its default value is random, and it can be arbitrary. Therefore, the pointer variable should be initialized at the same time it is created, either by setting the pointer to null or by pointing it to legitimate memory.

Second, after the pointer p is free or delete, it is not set to NULL, which makes the person mistakenly think P is a valid pointer. Although the names of free and delete are ferocious (especially delete), they simply release the memory that the pointer refers to, but do not kill the pointer itself. Usually, the statement if (P! = NULL) is used for error-proof handling. Unfortunately, the IF statement is not error-proof at this point because even if p is not a null pointer, it does not point to a valid block of memory.

Free () releases the memory that the pointer points to! Attention! The memory is released, not the pointer! This is very, very important! A pointer is a variable that is destroyed only at the end of the program. Once the memory space is freed, the pointer to the space is still there! Just now the pointer points to the content of the garbage, is undefined, so it is rubbish. So, as I've said earlier, after releasing the memory, point the pointer to null, preventing the pointer from being accidentally followed by a dereference. It's very important.

There are also issues to be aware of when using pointers:

1: Do not return a pointer or reference to the stack memory because the stack memory is freed at the end of the function.

2: Remember to assign a dynamic memory to the pointer before using the pointer for memory operation.

C + +: memory leaks and the concept of wild pointers

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.