当我尝试使用时strcpy
复制字符串给了我一个编译错误。
error C4996 'strcpy': This function or variable may be unsafe.
Consider using `strcpy_s` instead. To disable deprecation,
use `_CRT_SECURE_NO_WARNINGS`. See online help for details.
有什么区别strcpy
and strcpy_s
?
strcpy
是一个不安全的函数。
当您尝试使用复制字符串时strcpy()
如果缓冲区不够大,无法容纳它,则会导致缓冲区溢出。
strcpy_s()
is a 安全增强版 of strcpy()
.
With strcpy_s
您可以指定目标缓冲区的大小,以避免复制期间缓冲区溢出。
char tuna[5]; // a buffer which holds 5 chars incluing the null character.
char salmon[] = "A string which is longer than 5 chars";
strcpy( tuna, salmon ); // This will corrupt your memory because of the buffer overflow.
strcpy_s( tuna, 5, salmon ); // strcpy_s will not write more than 5 chars.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)