About C/C ++ Memory Management

Source: Internet
Author: User

Http://www.cnblogs.com/skynet/archive/2010/12/03/1895045.html this blog finally wrote five rules, although simple, but there are still some problems, here to explain.

Rule 1]After applying for memory with malloc or new, you should immediately check whether the pointer value is null. Prevent memory with NULL pointer values.

Occasional comment: refer to the instructions on malloc and new in Wikipedia. For the malloc mode in C language, it is acceptable to check null, but for the new operator (operator) in C ++ ), checking for null is basically useless, because C ++ has an exception mechanism, new will throw an exception STD: bad_alloc if not successful, how to handle can refer to http://msdn.microsoft.com/en-us/library/kftdy56f%28v=VS.71%29.aspx

Rule 2]Do not forget to assign initial values to arrays and dynamic memory. Avoid using uninitialized memory as the right value.

Even comment: it takes time to assign values to initial values. When initializing a memory memset in C language, pay attention to using buffersize * sizeof (object), and C ++ is more complicated, take care of constructor, new overload, and so on.

Rule 3]Avoid overrunning the subscript of an array or pointer, especially when "more than 1" or "Less 1" is performed.

Even comment: Generally, there will be no error when there is less than one runtime. For example, the memory size applied by the Microsoft compiler is generally larger than that specified by you. For example, if you apply for 10 bytes, for example, char * P = malloc (10);, normally P [10], p [11] operations are normal, so some strange phenomena may occur, that is, some bugs may occur on machines, and some people will not be able to reproduce them.

Rule 4]Dynamic Memory application and release must be paired to prevent memory leakage.

Even comment: It should be noted that new must be paired with delete, and malloc must be paired with free; otherwise, an error will occur. In addition, new [] must be deleted []. Otherwise, it will be leaked.

Rule 5]After the memory is released with free or delete, the pointer is immediately set to null to avoid "wild pointer ".

Even comment: this situation is generallyCodeFunction write is relatively long, the pointer is used again, or the pointer is global. Therefore, software companies generally require this encoding specification. If the function is very short and lean, there will be no such worry. Of course, I occasionally agree that this is beneficial and harmless.

Add some additional instructions:

For pointer type check, assert should not be used, but should be normal, if (! P) return e_pointer. Assert semantics and NULL pointer are two different things. If you do not want to judge or save trouble, you can use the const reference type in C ++, but it is not recommended to pass in the reference object and then modify it in the function body.

For memory operations, such as memset and memcpy, you must also note that you cannot access out-of-bounds data, similar to rule 3.

For C ++, new Delete can actually make a lot of tricks, I personally feel that it is not very useful, because the Server programming mostly uses C with the memory pool, heavy load new and other learning is rarely used. If you are interested in server-side memory management, you can read nginx code, which is simple, efficient, and practical.

In general, it should be who pulled the shit and who wiped the ass. That is to say, if an object or function has something new, it should be responsible for Delete. Otherwise, distance will not only produce beauty, but also generate bugs. (For example, if you create something new in AAA. cpp, you release it in BBB. cpp, or even release it in different places.ProgramThe structure is unknown and it is easy to produce bugs)

You can write some small software using Python or Lua to check the number of new, delete, malloc, and free items in the project. If the number does not match, there may be bugs, you can also check the pairing between new [] and delete. I have compiled such a small tool, which will be released after being improved.

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.