只是想知道(因为我们经常使用这些功能)。我看不出 strncpy() 之间有任何实际区别。和 memcpy() .难道不值得这么有效地说吗,
char* strncpy (char *dst, const char *src, size_t size)
{
return (char*)memcpy(dst, src, size);
}
还是我遗漏了任何副作用?有一个类似的earlier question , 但找不到确切的答案。
最佳答案
有区别,请参阅您链接到的 strncpy
页面的这一部分(强调我的):
Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
因此,如果要复制的字符串比限制短,strncpy
将用零填充,而 memcpy
读取超出限制(可能调用未定义的行为)。
关于c++ - strncpy() 是 memcpy() 的特例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6509449/