Effective C ++ (Chapter 2 Memory Management)

zhaozj2021-02-16  27

Chapter 2 Memory Management

The management problem of memory involved in C can be attributed to two aspects: it is correctly obtained and used effectively. A good programmer will understand why these two issues are listed in this order. Because the execution is fast, the program is small if it does not execute it as if you think, it is not used. "Correctly" means correctly calling memory allocation and release programs; "Effectively Using" is a memory allocation and release program that is written a particular version. Here, "correctly" seems to be more important.

However, it is true that C has inherited a very serious headache from C, which is the hidden danger of memory. Virtual memory is a good invention, but virtual memory is limited, not everyone can grab it first.

In C, as long as the memory allocated by Malloc is not returned with Free, memory leaks will be generated. In C , the name of the perpetrator is replaced with new and delete, but the situation is basically the same. Of course, because there is a session of the designer, the situation is slightly improved because the designer function provides a place where the object to be destroyed provides a convenient call. But this also brought more troubles because New and Delete are implicitly modulated constructor and destructive functions. And because the New and Delete operators can be defined within the class and the class, this has brought complexity and increasing an error. The following terms (also Terms M8) will tell you how to avoid the universal problems.