Difference between #39;strcpy#39; and #39;strcpy_s#39;?(“strcpy和“strcpy_s之间的区别?)
问题描述
当我尝试使用 strcpy
复制字符串时,它给了我一个编译错误.
When i tries to use strcpy
to copy a string it gave me a compile error.
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
和 strcpy_s
有什么区别?
推荐答案
strcpy
是一个不安全的函数.当您尝试使用 strcpy()
将字符串复制到不足以容纳它的缓冲区时,会导致缓冲区溢出.
strcpy
is a unsafe function.
When you try to copy a string using strcpy()
to a buffer which is not large enough to contain it, it will cause a buffer overflow.
strcpy_s()
是 strcpy()
的安全增强版.使用 strcpy_s
,您可以指定目标缓冲区的大小,以避免复制期间缓冲区溢出.
strcpy_s()
is a security enhanced version of strcpy()
.
With strcpy_s
you can specify the size of the destination buffer to avoid buffer overflows during copies.
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.
这篇关于“strcpy"和“strcpy_s"之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“strcpy"和“strcpy_s"之间的区别?


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01