Bjarne: how to deal with Memory leakage?

Source: Internet
Author: User

Write out those that will not cause any memory leakageCode. Obviously, when your code is full of new operations, delete operations, and pointer operations, you will get dizzy somewhere, resulting in Memory leakage and pointer reference errors, and other issues.

This is totally irrelevant to how you treat memory allocation with caution: the complexity of the Code will always exceed the time and effort you can make. As a result, some successful techniques were developed, depending on
Allocations and deallocation are hidden behind manageable types. Standard
Containers) is a good example. Instead of managing the memory for the elements, they avoid bad results. Imagine there is no string or vector
To write this:

# Include <vector>
# Include <string>
# Include <iostream>
# Include <algorithm>

Using namespace STD;

Int main () // small program messing around with strings
{
Cout <"Enter some whitespace-separated words:/N ";
Vector <string> V;
String S;
While (CIN> S) v. push_back (s );
Sort (V. Begin (), V. End ());
String cat;
Typedef vector <string >:: const_iterator ITER;
For (iter p = V. Begin (); P! = V. End (); ++ p) Cat + = * P + "+ ";
Cout <cat <'/N ';
}


How many opportunities do you get the right results for the first time? How do you know that you have not caused Memory leakage?

Note that no explicit memory management, macros, shapes, overflow checks, explicit length restrictions, and pointers appear. By using function objects and standardsAlgorithm(Standard algorithm), I can avoid using Pointers-for example, using an iteration sub (iterator), but for a smallProgramIt's a bit difficult.



These skills are not perfect. It is not always that easy to systematically use them. However, there is an astonishing difference between applying them, and by reducing the number of explicit memory allocations and reallocation, you can even
The remaining examples are more easily tracked. As early as 1981, I pointed out that by reducing the number of objects that I had to explicitly track from tens of thousands to dozens, efforts were made to make the program run correctly from terrible
It became manageable and even easier.

If your program does not include a library that minimizes explicit memory management, you need to complete and run your program correctly, the fastest way may be to create such a database first.

Templates and standard libraries implement containers, resource handles, and such things. They were used earlier or even years ago. Abnormal use makes it more perfect.


You can use resource
Handle) to minimize the possibility of Memory leakage. Here is an example: I need to use a function to create an object in idle memory and return it. At this time, you may forget to release this object. Bi
Actually, we cannot say that we only focus on who will be responsible for the release of this pointer. Use the resource handle. Here auto_ptr in the standard library is used to make it clear
.

# Include <memory>
# Include <iostream>

Using namespace STD;

Struct s {
S () {cout <"make an S/N ";}
~ S () {cout <"destroy an S/N ";}
S (const S &) {cout <"Copy Initialize an S/N ";}
S & operator = (const S &) {cout <"Copy assign an S/N ";}
};

S * F ()
{
Return new S; // who should release this s?
};

Auto_ptr <S> G ()
{
Return auto_ptr <S> (new S); // explicit transfer is responsible for releasing this s
}

Int main ()
{
Cout <"Start main/N ";
S * P = f ();
Cout <"After F () before G ()/n ";
// S * q = g (); // will be captured by the compiler
Auto_ptr <S> q = g ();
Cout <"Exit Main/N ";
// * P causes memory leakage
// * Q is automatically released
}

In a more general sense, consider resources, not just memory.


if you cannot systematically apply these skills in your environment (for example, you must use code elsewhere, or another part of your program is written by the primitive humans, such as
neandthals, niandte people, and ape people widely distributed in Europe in the Old Stone Age ), use a memory leak detector as part of the development process, or
insert a garbage collector (Garbage Collector ).

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.