Is strncpy() a specialization of memcpy()?

后端 未结 5 1543
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 01:59

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,

         


        
相关标签:
5条回答
  • 2021-01-25 02:14

    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.

    0 讨论(0)
  • 2021-01-25 02:16

    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).

    0 讨论(0)
  • 2021-01-25 02:16

    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

    0 讨论(0)
  • 2021-01-25 02:26

    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.

    0 讨论(0)
  • 2021-01-25 02:27

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

    0 讨论(0)
提交回复
热议问题