Brad Fitzpatrick (brad) wrote,
Brad Fitzpatrick
brad

I've just spent an hour debugging part of our operating system project and finally now just traced it down to my misunderstanding of the strncpy() function....

DESCRIPTION
The strcpy() function copies the string pointed to be src (including the terminating `\0' character) to the array pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy.

The strncpy() function is similar, except that not more than n bytes of src are copied. Thus, if there is no null byte among the first n bytes of src, the result wil not be null-terminated.

In the case where the length of src is less than that of n, the remainder of dest will be padded with nulls.
Why does it null-pad it? I don't see that as being particularly beneficial. I don't mind it... and now that I understand that n should be the MIN(src, dst) it's all good... but it seems unnecessary. Evan? Scott?
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 3 comments