On the C / C + + memory management number of Luanjiang

2010-12-03  来源:本站原创  分类:CPP  人气:94 

http://www.cnblogs.com/skynet/archive/2010/12/03/1895045.html write this blog the last five rules, though simple, but still some problems, here a few words.
【1】 rules apply with malloc or new memory, you should immediately check whether the pointer value NULL. NULL pointer value to prevent the use of memory.
Dual Review: Reference wiki encyclopedia for malloc and new instructions, for the C language malloc way, check the NULL is possible, but for C + + the new operator (operator), check the NULL is basically useless, because the C + + has exceptions, unsuccessful will throw new Exception std:: bad_alloc, how can refer http://msdn.microsoft.com/en-us/library/kftdy56f% 28v = VS.71% 29.aspx
【2】 Do not forget the rules and dynamic memory for the array of initial values. Prevent uninitialized memory as the right value to use.
Dual Review: Initial assignment is to spend time, C language and initialize a buffer memset time to pay attention with buffersize * sizeof (Object), and C + + is more complex, such as the constructor, new overload of the class, be careful.
【3】 rules to avoid array subscript out of bounds or pointer, in particular, be careful happen "more than 1" or "less 1" operation.
Dual Review: A little more than a general run-time error is not certain, such as Microsoft, the compiler will allocate memory generally larger than you specify, such as your application 10bytes, such as char * p = malloc (10);, usually operating p [10], p [11] Han is no problem, so be some strange phenomenon, that is, the machine will be out some bug some people, some people can not reproduce.
【4】 rules apply and release the dynamic memory must be paired, prevent memory leaks.
Dual Review: want to clarify is, new and delete must match, malloc and free must match, or mistakes. Another new [], be sure to delete [], or will leak.
【5】 by rules or delete the release of free memory immediately after the pointer is set to NULL, prevent "wild pointer."
Dual Review: This function is generally written in the code longer, the pointer used by another, or the pointer is a global situation. Therefore, the general software companies are hard coding standards required to do so, if the function is very energetic, in fact, no such concern. Of course, even agreed to do so hurt.
Then add some additional instructions:
Check for pointer types should not be used assert, but rather normal operation, if (! P) return E_POINTER; this. Use assert null pointer semantics and judgments are two different things. If you do not want to judge or the easy way, C + + const reference type can be used, but does not recommend passing reference object, and then modified in the function body.
For memory operations, such as memset, memcpy, the same can not be accessed out of bounds to pay attention to the data, similar to rule 3.
For C + +, new delete can actually make a lot of tricks, personal feeling of little use, mostly because the server-side programming using C with the memory pool, heavy learned like new rarely used. If you are interested in memory management on the server side, you can read the code nginx, simple and efficient and practical.
In general, who is pulling the feces should be who the backside, an object or function that is something new, it should be responsible for the delete, or generated from not only the United States, there will be a bug. (More nonsense about, for example, in aaa.cpp something new inside, and then you release it in the bbb.cpp inside, or even release it in different places, do not know if the program structure, it is prone to bug)
You can write some small python or lua software, check the project new and delete, malloc and free number, if the numbers do not match, there might be bug, you can also check the new [] and delete [] matching conditions . I have written such a small tool to improve the future released.

  • On the C / C + + memory management number of Luanjiang 2010-12-03

    http://www.cnblogs.com/skynet/archive/2010/12/03/1895045.html write this blog the last five rules, though simple, but still some problems, here a few words. [1] rules apply with malloc or new memory, you should immediately check whether the pointer v

  • Java memory management (1. Memory allocation) 2010-03-29

    On the Java memory allocation, and many problems are vague and can not run through a comprehensive understanding. This access to information, desires dig thoroughly clarify java memory allocation context, simply because the level is limited, did not

  • Memory management and features 2010-03-29

    #import <Foundation/Foundation.h> #import "RetainTracker.h" /** Renhe Gang released without the use of memory, otherwise we Jiang may erroneously the data the old, to rise to a variety of Cuowu, but if Gai memory has been loaded by other d

  • Java-based brush up - memory management. Garbage Collection 2010-05-03

    -------------------------------------------------- -------------------------------------------- Reference: "Java2 Programming Xiangjie" http://www.ibm.com/developerworks/cn/java/l-JavaMemoryLeak/ -------------------------------------------------

  • Fundamentals of Java memory management - Java memory allocation 2009-11-09

    1. Introduction Now extract a Java5 memory management white paper statement: One strength of the Java ™ 2 Platform , Standard Edition (J2SE ™ ) is that it performs automatic memory management, thereby shielding the developer from the complexity of ex

  • Application Server JVM memory management and optimization of the SUN JVM memory management (under) 2010-04-16

    Analysis: Jason SHChen How to adjust the JVM parameters of the system in a specific hardware configuration to achieve optimal performance. Part SUN JVM through the memory management mechanism of introduction, we all know that the SUN JVM memory into

  • . Net memory management and garbage collection 2010-04-20

    . NET Framework's garbage collector management application memory allocation and release of. Every time you use the new operator to create an object, the runtime objects are allocated from the managed heap memory. Managed heap as long as the address

  • Linux System Programming Study Notes (7) memory management 2010-05-01

    Memory management: For a process, the memory is the most basic and most important resources. Memory management, including: memory allocation, memory operations and memory release. 1, the process address space: Linux to virtualization of physical memo

  • MemCached memory management mechanism 2010-05-29

    MemCached used with optimized memory management, memory allocation makes the object very much improved efficiency. 1. The traditional shortcomings of memory management The traditional methods of memory allocation to the OS for use mallocate memory, t

  • C language memory management 2010-06-09

    Programmers often write memory management procedures, often on tenterhooks. If you do not want to mine, the only solution is to find all the hidden mines and exclude them, hide is get let off of. The contents of this chapter should be further than th

  • Linux Kernel Design and Implementation Notes - memory management 2010-06-19

    11.1 kernel to the physical page as the basic unit of memory management. Memory management unit (MMU, memory management and the virtual addresses into physical addresses of the hardware) are usually handled as a unit page. Because of this, MMU to pag

  • Android procedures required reading for memory management 2010-06-30

    Many developers are coming from on J2ME or J2EE, and for the use and understanding of memory is not quite in place, Android Development Network to give us a framework in this guidance, to prevent the emergence of shabby. Android to Java as a language

  • Linux kernel for virtual memory management (1) 2010-07-13

    Linux kernel for virtual memory management is a process-based, each process has its own virtual memory space. The system space is shared by all processes. On the virtual space used to describe the vm_area_struct data structures. struct vm_area_struct

  • Windows memory management study note (2) 2010-07-14

    Need to know before using virtual memory management information system memory and virtual address space of the relevant information. 1. Management System Information - page size, size distribution of the basic system information, CPU information mana

  • iPhone / Mac Objective-C memory management tutorials and theory analysis 2010-07-30

    Copyright Statement Of this article belongs to Vince Yuan (vince.yuan # gmail.com) for all. Welcome nonprofit reproduced, reprinted must contain the original link http://vinceyuan.cnblogs.com/ , and must include the complete content of this copyright

  • Kernel comparison: 2.6 kernel, improved memory management 2010-08-06

    With the Linux kernel development and mature, more users can look forward to running Linux system to handle very large applications or even scientific analysis of large databases. These enterprise applications typically require large amounts of memor

  • Inside memory management 2010-08-06

    Why must manage memory Memory management is the most fundamental areas of computer programming one. In many scripting languages, you do not worry about how memory is managed, this does not make the importance of memory management a little bit lower.

  • Windows Memory Management 2010-08-08

    Original Address: http://www.cic.tsinghua.edu.cn/jdx/book4/dlz.htm Memory management, high efficiency for the preparation of the Windows program is very important, it is Because Windows is a multi-task system, its memory management and single task of

  • Java memory management features 2010-08-13

    Java One of the biggest advantages is the abolition of the pointer, the garbage collector to automatically manage memory recovery. Programmers do not need to release the memory by calling the function. 1, Java's memory management is the allocation an

  • SUN JVM memory management and server memory allocation and optimization of non- 2010-08-16

    Part for a briefing on SUN JVM memory management mechanism. This mainly explains the JVM with performance-related parameters, how to use tools to monitor JVM memory allocation and how the use of JVM parameters to adjust the system in a specific hardw