'strcpy'和'strcpy_s'之间的区别? [英] Difference between 'strcpy' and 'strcpy_s'?

查看:1256
本文介绍了'strcpy'和'strcpy_s'之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图使用'strcpy'来复制一个字符串,它给了我一个编译错误。

When i tries to use 'strcpy' to copy a string it gave me a compile error.

错误C4996'strcpy':此函数或变量可能不安全。
考虑使用strcpy_s。要禁用弃用,
使用_CRT_SECURE_NO_WARNINGS。

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'有什么区别?

What is the difference between 'strcpy' and 'strcpy_s'?

推荐答案

strcpy是一个不安全的功能。
当尝试使用strcpy()将一个字符串复制到一个不足以容纳它的缓冲区时,会导致一个缓冲区溢出。

strcpy is a unsafe funtion. 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'之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆