再谈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.

猜你喜欢

转载自blog.csdn.net/jangdong/article/details/81297360