High quality C ++ programming point drop (1)

zhaozj2021-02-08  370

High quality C programming point drop (1)

Effectively solve memory leaks

First, you need a function to assign an array to the equorition number and will use it outside the function.

unreasonable:

INT * GetArray (int N)

{

INT * p = new int [n];

For (int i = 0; i

{

P [I] = i;

}

Return P;

}

reasonable:

Void getaRray (int * p, int N)

{

For (int i = 0; i

{

P [I] = i;

}

}

Analysis:

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 rt [n];

Private:

INT * M_PPTR;

}

reasonable:

Class A

{

PUBLIC:

A (void): m_pptr (0) {}

~ A (void) {clear ();

Bool create (INT N) 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)

{

IF (m_pptr)

Return False;

m_pptr = new int tent [n];

For (int i = 0; i

{

M_pptr [i] = 3 / a [i];

}

Return True;

}

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) {_ type * 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 in);

For (int i = 0; i

{

IF (0 == a [i])

{

Return False;

}

Ptrguard .get () [i] = 3 / a [i];

}

m_pptr = ptrguard.release ();

Return True;

}

Analysis:

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.

转载请注明原文地址:https://www.9cbs.com/read-152.html

New Post(0)