First, you need a function to assign an array to the equorition number and will use it outside the function.
unreasonable:
INT * GetArray (int * p = new int [n]; for (int i = 0; i reasonable: Void getArray (int * p, int N) {for (int i = 0; i Checking the best way for memory leaks is to check the full pairing application and release, apply in the function and release in external release, which will cause the consistency of the code to be poor, and it is difficult to maintain. Moreover, the function you write is not necessarily yourself, such a function, others do not know how appropriate, if it is a DLL export function, and you can use it under different platforms, it will cause the system. collapse. The best solution is to apply the memory in the outside of the function call, and the function is only copied to the data. Second, you need to write a class to manage a pointer for you, this class will package the application memory, release, and other basic operations of the pointer. unreasonable: Class a {public: a (void) {} ~ a (void) {delete [] m_pptr;} void create (int N) {m_pptr = new int [n];} private: int * m_pptr;}; reasonable: Class a {public: a (void): m_pptr (0) {} ~ a (void) {clear ();} bool create (int N) {if (m_pptr) return false; m_pptr = new int [n]; return Ture;} void clear (void) {delete [] m_pptr; m_pptr = 0;} private: int * m_pptr;}; Analysis: The unreasonable code is that when you repeat CREATE, it will cause memory leaks, and the solution is to determine if the pointer is 0 before New. To be able to perform this judgment, you must initialize the pointer when constructed, and add a CLEAR function to this class to release memory. Third, the resulting Create function, you now need to do some complicated algorithm operations according to the incoming parameters, and assign values for the array of applications. unreasonable: BOOL CREATE (INT * A, INT N) RETURN FALSE; m_pptr = new int [n]; for (int i = 0; i reasonable: Template Class Auto_Array {public: explicit auto_Array (_ty * pptr = 0) throw (): m_ptr (pptr) {} ~ auto_array () {delete [] m_ptr;} void reset (_ty * pptr = 0) {IF (PPTR! = m_Ptr) {delete [] m_Ptr; m_Ptr = pPtr;}} _Ty * release (void) {_ Ty * pTemp = m_Ptr; m_Ptr = 0; return pTemp;} private: auto_array (const auto_array & other) {} auto_array & operator = (const Auto_Array & Other) {}_ty * m_ptr;}; Bool A :: Create (INT * A, INT N) {IF (M_pptr) Return False; Auto_Array Ptrguard (new int = 0; i In the loop, when a value in the parameter array A is 0, it will produce 0 abnormalities, then this will cause you to release the memory that is the m_pptr application on the above. In order to solve this problem, we wrote an Auto_Array as a pointer to the guards to see attempt to escape. It will delete the memory pointer attached to it at the time of Auto_Array object PtrGuard. We first use ptrguard to perform all pointer operations, and then assign the pointer to the true variable in the final end of the operation, and give the PTRGUARD to the attachment to the pointer, so that we get a safest result. In addition, it is important to note that C STL reservoir has a template auto_ptr, but it only supports the memory of a single object, does not support arrays, write such an auto_Array is also no. 4. You need to open a memory to store and manage a matrix of 4 x 4, and unitize. unreasonable: Int Amatrix [4] [4]; for (int i = 0; i <4; i ) {for (int J = 0; j <4; j ) {if (i == j) {AMATRIX [i] [ j] = 1;} else {Amatrix [i] [j] = 0;}}} reasonable: Int Amatrix [4 * 4]; for (int i = 0; i <4; i ) {for (int J = 0; j <4; j ) {if (i == j) {AMATRIX [i * 4 j] = 1;} else {Amatrix [i * 4 j] = 0;}}} Analysis: Avoid using multi-dimensional arrays at any time, the increase in array dimension, the corresponding program complexity will be geometric The number of ways increases, and it is also more difficult to understand. 5. You need to assign a value on the above matrix, which gives it to assign it from the upper left corner to the lower right corner. unreasonable: For (int i = 0; i <4; i ) {for (int J = 0; j <4; j ) {Amatrix [j * 4 i] = i * 4 j;}} reasonable: For (int i = 0; i <4; i ) {for (int J = 0; j <4; j ) {Amatrix [i * 4 j] = j * 4 i;}} Analysis: Try to ensure the order of access arrays of each element. Due to the management mode of Windows memory, memory is managed by paging. Order access arrays can basically ensure that the page will not switch back, thereby reducing the number of page failures and improves the overall performance of the program. This performance improvement is particularly obvious for large arrays. Sixth, you need to use three float values to represent a three-dimensional point and write a function to calculate the array of three-dimensional points. unreasonable: Void foo (float * ppoints [3]) {float apoint [3] = {1.0F, 2.0F, 3.0F}; int ncount = (int) _msize (ppoints); for (int i = 0; i reasonable: Struct Point3 {float x, y, z;}; void foo (point3 * ppoints, int ncount) {POINT3 PT = {1.0F, 2.0F, 3.0F}; for (INT i = 0; i } Analysis: There are two points. When designing such a function that needs to be passed into an array, don't forget that the number of elements of the array is also incoming, even if it is fixed, this will be a good habit. Second, for Float [3], try to avoid direct use, the best way is to use Struct to simply encapsulate it, and use "=" directly in copying. 7. You have a definition of a function, in which a relatively large object DATA is in this function, and delete it after calculation. But this function will be called frequently. unreasonable: Void foo (void) {data * p = new data; calcdata (p); delete p;} reasonable: CHAR BUF [SIZEOF (DATA)]; Void foo (void) {data * p = new (buf) data; Calcdata (P);} Analysis: New (buf) type; is a positioned new syntax, which does not real allocate memory, but simply divides a space that matches the type size at the specified memory start point, and directly on this memory This type is constructed, and the pointer to the object is returned. Since it has no real allocation of memory space, its efficiency is very high, in similar to the above routine, frequent application and release a large object, the positioned New can bring a large efficiency.