C++中strcpy为什么 总是 “警告”

youxin2012 2012-11-13 07:17:37
问下我的 strcpy为什么 总是 “警告” warning C4996: 'strcpy' was declared deprecated 啊

代码如下,我用的 是VS2005
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
char arr1[]={"abcdefghi"};
char arr2[]={"ABCDEFG"};
//memset(arr1,0,5);
strcpy(arr1,arr2);

cout<<arr1;
cin.get();

return 0;
}
...全文
1612 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongwenjun 2012-11-13
  • 打赏
  • 举报
回复
GCC 就不警告. strcpy 效率高,要求调用者自己负责
hjrui09 2012-11-13
  • 打赏
  • 举报
回复
strcpy 本来就是不安全的函数! 因为使用者不能指明什么时候拷贝结束,只能编译器自动识别'\0'结束。 用得不好的时候会出错,所以才警告嘛。这个东西见仁见智,看使用者的水平
辰岡墨竹 2012-11-13
  • 打赏
  • 举报
回复
其实strncpy也是不安全的,如果缓冲区空间不够,它不会自动添加\0结束符。 而strncpy_s可以让用户选择截断方式。 所以微软才推出了_s系列函数,如果你不考虑移植到Linux下,尽量用这些安全函数。 #define _CRT_SECURE_NO_DEPRECATE 这个不要用。
qq120848369 2012-11-13
  • 打赏
  • 举报
回复
引用 4 楼 youxin2012 的回复:
谢谢你。 另外你的strncpy 是自己定义的吧。 我引用 cstring库中的 strncpy还是出现 警告啊 warning C4996: 'strncpy' was declared deprecated #include <iostream> #include <cstring> //#define _CRT_SECURE_NO_DEPRECATE……
NAME
       strcpy, strncpy - copy a string

SYNOPSIS
       #include <string.h>

       char *strcpy(char *dest, const char *src);

       char *strncpy(char *dest, const char *src, size_t n);

DESCRIPTION
       The  strcpy()  function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest.  The
       strings may not overlap, and the destination string dest must be large enough to receive the copy.  Beware of buffer overruns!  (See BUGS.)

       The strncpy() function is similar, except that at most n bytes of src are copied.  Warning: If there is no null byte among the first n bytes of
       src, the string placed in dest will not be null-terminated.

       If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written.

       A simple implementation of strncpy() might be:

           char *
           strncpy(char *dest, const char *src, size_t n)
           {
               size_t i;

               for (i = 0; i < n && src[i] != '\0'; i++)
                   dest[i] = src[i];
               for ( ; i < n; i++)
                   dest[i] = '\0';

               return dest;
           }

RETURN VALUE
       The strcpy() and strncpy() functions return a pointer to the destination string dest.
sumos 2012-11-13
  • 打赏
  • 举报
回复 1
deprecated 表示 不建议使用。 现在一般改为strcpy_s安全性较高的函数了。
FrankHB1989 2012-11-13
  • 打赏
  • 举报
回复
引用 4 楼 youxin2012 的回复:
谢谢你。 另外你的strncpy 是自己定义的吧。 我引用 cstring库中的 strncpy还是出现 警告啊 warning C4996: 'strncpy' was declared deprecated #include <iostream> #include <cstring> //#define _CRT_SECURE_NO_DEPRECATE……
_s是M$提交标准化的扩展,正式定义在ISO/IEC TR 24731-1。 strncpy是C99和C++98起的库函数,所谓deprecated是M$自己扯的。 #define _CRT_SECURE_NO_DEPRECATE放最前面。
youxin2012 2012-11-13
  • 打赏
  • 举报
回复
谢谢你。 另外你的strncpy 是自己定义的吧。 我引用 cstring库中的 strncpy还是出现 警告啊 warning C4996: 'strncpy' was declared deprecated #include <iostream> #include <cstring> //#define _CRT_SECURE_NO_DEPRECATE using namespace std; int main() { char arr1[]={"abcdefghi"}; char arr2[]={"ABCDEFG"}; //memset(arr1,0,5); strncpy(arr1,arr2,4); //cout<<arr1; cin.get(); return 0; }
qq120848369 2012-11-13
  • 打赏
  • 举报
回复
因为strcpy不判断dest的buffer长度,可能因为src长于dest而造成dest写溢出。 用strncpy是永远的选择,strcpy已经很久没有出现在我的代码里了。 看一下它的实现吧:
       A simple implementation of strncpy() might be:

           char *
           strncpy(char *dest, const char *src, size_t n)
           {
               size_t i;

               for (i = 0; i < n && src[i] != '\0'; i++)
                   dest[i] = src[i];
               for ( ; i < n; i++)
                   dest[i] = '\0';

               return dest;
           }
注意永远给buffer - 1的位置封0, 无论snprintf返回几。
转角天边 2012-11-13
  • 打赏
  • 举报
回复
引用 1 楼 zhuankeshumo 的回复:
http://bbs.csdn.net/topics/390279925看一下 一样的
newtee 2012-11-13
  • 打赏
  • 举报
回复

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧