这是我的代码:
#include <iostream>
#include <assert.h>
using namespace std;
char * my_strcpy(char *dst,const char *src)
{
assert(dst != NULL);
assert(src != NULL);
char *ret = dst;
while((* dst++ = * src++) != '\0');
return ret;
}
int main()
{
char str[4]="abc";
cout<<str<<endl;
strcpy(str+1,str); //works, "aabc" will be in it, \0 is out of its range
cout<<str<<endl;
my_strcpy(str+1,str); //an error will be occured here as expected
cout<<str<<endl;
return 0;
}
我的问题是,为什么微软的 strcpy 支持重叠复制并带来潜在的泄漏风险,即使 c++ 标准库声明 strcpy 不支持重叠场景?这种设计有什么优势吗?
谢谢!