Is strncpy() a specialization of memcpy()?

陌路散爱 提交于 2019-12-20 04:40:29

问题


Just curious to know (as we use these functions often). I don't see any practical difference between strncpy() and memcpy(). Isn't it worth to say that effectively,

char* strncpy (char *dst, const char *src, size_t size)
{
  return (char*)memcpy(dst, src, size);
}

Or am I missing any side effect? There is one similar earlier question, but couldn't find an exact answer.


回答1:


There is a difference, see this part of the strncpy page you linked to (emphasis mine):

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.

So if the string to be copied is shorter than the limit, strncpy pads with zero while memcpy reads beyond the limit (possibly invoking undefined behaviour).




回答2:


No, they are not the same.

From the C Standard (ISO/IEC 9899:1999 (E))

7.21.2.3 The strcpy function

Description

2 The strncpy function copies not more than n characters (characters that follow a null character are not copied) from the array pointed to by s2 to the array pointed to by s1.260) If copying takes place between objects that overlap, the behavior is undefined.
3 If the array pointed to by s2 is a string that is shorter than n characters, null characters are appended to the copy in the array pointed to by s1, until n characters in all have been written.
Returns
4 The strncpy function returns the value of s1.

7.21.2.1 The memcpy function
Description

2 The memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.
Returns 3 The memcpy function returns the value of s1.

when using memcpy() the source and destination buffers can overlap, while in strncpy() this must not happen.

According to the C standard, the behavior for overlapping buffers are undefined for both strncpy() and memcpy().

According to the C standard, the real difference between strncpy() and memcpy() is that if the source string is less then N value, then NULL characters are appended to the remaining N quantity.

memcpy() is more efficient, but less safe, since it doesn't check the source to see if it has N quantity to move to the target buffer.




回答3:


No, strncpy() is not a specialization, since it will detect a '\0' character during the copy and stop, something memcpy() will not do.




回答4:


Adding on to what the others have said, the type of the src and dst pointers does not matter. That is, I can copy a 4 byte integer to 4 consecutive characters of 1 byte like this:

int num = 5;
char arr[4];
memcpy(arr, &num, 4);

Another difference is that memcpy does not look ofr any characters (such as NULL, by strncpy). It blindly copies num bytes from source to destination.

Edited: Properly formatted the code




回答5:


You could potentially make strncpy faster by checking for a \0 and not copying past that point. So memcpy would always copy all the data, but strncpy would often be faster because of the check.



来源:https://stackoverflow.com/questions/6509449/is-strncpy-a-specialization-of-memcpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!