The functions of the strcpy_s and strcpy () functions are almost the same. The strcpy function, like the Get function, has no method to guarantee a valid buffer size, so it can only assume that the buffer is large enough to hold the string to be copied. This leads to unpredictable behavior when the program is running. These unpredictable behaviors can be avoided by strcpy_s.
strcpy_s is a secure version of strcpy, and it is safe because it has cross-border checks when copying strings. The following is the implementation code for strcpy_s, which can be found in the Tcscpy_s.inl file:
/****tcscpy_s.inl-general Implementation of _tcscpy_s** Copyright (c) Microsoft Corporation. All rights reserved.**purpose:* This file contains the general algorithm for strcpy_s and its variants.*****/_func_prologueerrno_t __cdecl _func_name (_char*_dest, size_t _size,Const_char *_src) {_char*p; size_t available; /*Validation Section*/_validate_string (_dest, _size); _validate_pointer_reset_string (_SRC, _dest, _size); P=_dest; Available=_size; while((*p++ = *_src++)! =0&&--available >0) { } if(Available = =0) {_reset_string (_dest, _size); _return_buffer_too_small (_dest, _size); } _fill_string (_dest, _size, _size-Available +1); _return_no_error;}
_validate_string should be the validity of the validation string, whether null-terminated.
_validate_pointer_reset_string should be the original information for the record string so that the copy fails to recover later.
When the destination space is insufficient, the string is recovered based on the information recorded by the _validate_pointer_reset_string, and errors are reported (in debug mode) as a pop-up dialog box.
_fill_string completes the work of adding a null terminator at the end of the string.
Correct usage:
intn =6; Char**argsmy =New Char*[n]; intMaxLen = -; for(inti =0; I < n; i + +) {Argsmy[i]=New Char[MaxLen];//Args[i];} strcpy_s (argsmy[1],maxlen,"e"); strcpy_s (argsmy[2],maxlen,"Lzma_"); strcat_s (argsmy[2], -, Ctappenctop.getbitstreamfile ()); strcpy_s (argsmy[3],maxlen,"-BS12"); strcpy_s (argsmy[4],maxlen,"-cn0"); strcpy_s (argsmy[5],maxlen,"-d15");
Error usage:
argsmy[2"lzma_"; strcpy_s (argsmy[2],maxlen,"See");
Reason:
ARGSMY[2] = "Lzma_"; Because Argsmy[2] is a pointer. He pointed to an allotted space, the length maxlen.
When this is assigned, the pointer points to a position that is changed. And then strcpy_s (Argsmy[2],maxlen, "see"); The constant variable space is actually forced to assign a value. So the problem.
strcpy_s usage:
errno_t strcpy_s ( Char *strdestination, size_t numberofelements, const Char *<size_t size> errno_t strcpy_s ( char (& strdestination) [size], constChar *// C + + only
Example:
//Crt_strcpy_s.cpp//This program uses strcpy_s and strcat_s//To build a phrase.//#include<string.h>#include<stdlib.h>#include<stdio.h>#include<errno.h>intMainvoid ){ Char string[ the]; //using template versions of strcpy_s and strcat_s:strcpy_s (string,"Hello World from" ); strcat_s (string,"strcpy_s" ); strcat_s (string," and" ); //Of Course we can supply the size explicitly if we want to:strcat_s (string, _countof (string),"strcat_s!" ); printf ("String =%s/n",string );}
The strcpy_s of c++string function