According to the "Pen test questions" in the frequency of frequencies "Cphj (original), and many netizens. I have done the following summary: Most people agree to the following: Just personal style is somewhat different. Char * STRCPY (CHAR * DEST, Const Char * src) {Assert (null! = Dest); assert (null! = Src);
Char * TMP = DEST; while ('/ 0'! = (* TMP = * src ) // Because * TMP is not a Boolean value, it is necessary to compare
Return (DEST);
According to the definition in MS as follows: (% vC% / vc7 / crt / src / intel / strat.asm) Page; ***; char * STRCPY (DST, SRC) - Copy One String over another ;; Purpose :; Copies the string src info the spot specified by; design ;; algorithm :; char * STRCPY (CHAR * DST, CHAR * SRC); {; Char * CP = DST ;;; WHILE (* CP = * SRC ); / * copy src over dst * /; return (dst) ;;} ;; entry :; char * dst - String over Which "src" is to be copied; const char * src - string to be copied over "DST" ;; exit :; The Address of "DST" in EAX ;; Uses :; Eax, ECX ;; Exceptions:; ******************* ********************************************************
1. No two pointers entered are valid. 2. No two strings are ending with NULL. 3. Does not check if the space of the target pointer is greater than the space equal to the original string.
So these conditions require caller to complete. Look at the following program: // Copy string "abcd" int Nlen_src = 0; int Nlen_Dest = 0; char sztemp [] = "1234567890"; char szsrc [] = {'a', 'b', 'c', ' D '}; // The correct usage is to add a' / 0 'char szdest [2]; // char szdest [5] = {0}; the minimum // boundary of allocation space in the stack is 4 bytes, so The SZDEST array is 1, 2, // 3, 4 below the results below. NLEN_SRC = Strlen (SZSRC); NLEN_DEST = Strlen (SZDEST); STRCPY (SZDEST, SZSRC); result is: NLEN_SRC = 14, NLEN_DEST = 18; szdest = "abcd1234567890", szsrc = "1234567890"; szTemp = "567890"; The array pointer offset causes other variable content in the stack to change. So pay attention to the following points when using Strcpy: 1. When the string space assigned in the array in the stack, its size must be more than one of the character strings to be loaded to store the ended NULL character (represented by {0} and '/ 0'), and to initialize immediately . 2. The space of the target string must be greater than the space equal to the original string, otherwise it is recommended to use Strncpy () or WinAPI LPSTRCPYN () 3. Note When using LPSTR LSTRCPYN (LPTSTSTR LPSTRING1, LPCTSTR LPSTSTRING2, INT IMAXLENGTH), the IMAXLEngth is the string length plus 1 to copy 1 because it automatically sets the last character of LPString1 to NULL. This avoids the string pointer. 4. The space allocated with the New is also initialized in time. Use Memset () or ZeromeMory ().
-------- Related information can be referred to << High Quality C Programming Guide >> (Dr. Lin Rui)