再談wcscpy_s函數異常

int _tmain(int argc, _TCHAR* argv[])
{
	WCHAR ch[2]={0};
	wcscpy_s(ch,2,TEXT("12")); //1、eroo 需要空間 要3個字節  +  ‘\0’

	WCHAR ch1[1]={0};
	wcscpy_s(ch1,1,TEXT("12")); //2、erro 存儲空間小於 src 

	//1 2兩種情況都會報錯的!!!


	WCHAR ch2[3]={0};
	wcscpy_s(ch2,3,TEXT("12"));  //ok  此函數拷貝安全的,但是還是會拋出異常的!!

	return 0;
}


The strcpy_s function copies the contents in the address of strSource, including the terminating null character, to the location specified by strDestination. The destination string must be large enough to hold the source string, including the terminating null character. The behavior of strcpy_s is undefined if the source and destination strings overlap.

wcscpy_s and _mbscpy_s are wide-character and multibyte-character versions of strcpy_s respectively. The arguments and return value of wcscpy_s are wide character strings; those of _mbscpy_s are multibyte character strings. These three functions behave identically otherwise.

If strDestination or strSource is a null pointer, or if the destination string is too small, the invalid parameter handler is invoked as described in Parameter Validation. If execution is allowed to continue, these functions return EINVAL and set errno to EINVAL.

Upon successful execution, the destination string will always be null terminated.

In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads.

The debug versions of these functions first fill the buffer with 0xFD. To disable this behavior, use _CrtSetDebugFillThreshold.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章