strcpy and strncpy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boba
    New Member
    • Feb 2010
    • 4

    strcpy and strncpy

    Hi,

    I am a newbie to C++ (and programming in general)

    I have the following:

    char* fOutMsg;
    strcpy(fOutMsg, "03DS2");
    --------------------------------------------------------------
    in another place:

    char fPpn[32] = {0};
    strcpy(fPpn, " ");

    -------------------------------------------------------------------
    in another location:

    char fStr;
    struct* ioEx;
    strcpy(fStr, W2A(ioEx->authcode));

    ---------------------------------------------------------------------
    more:

    char* iProdType;
    strcpy((char*) fDef.prodtype, iProdType);

    ------------------------------------------------------------------------
    and:

    strcpy(fI8_GPID , "%");


    So as you can see, I have different parameters for strcpy in different places in the code, I was wondering how to substitute strcpy with strncpy.

    How does it work? could you please provide me real examples based on my above data? As I said, I am new to C++ and I don't know how to terminate the buffer with null or not so please be patient with me

    Thanks
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    First let's look at the function prototypes:
    Code:
    char *strcpy(char *destination, const char *source);
    char *strncpy(char *destination, const char *source, size_t num);
    Both functions copy the source string to the destination buffer. The difference is that strncpy copies at most num characters. The purpose of this limitation is to insure that you don't write past the end of the buffer.

    There are two things you need to do to change your code to use strncpy instead of strcpy:
    • Decide what num value to use. That's easy -- set it to the size of the destination buffer.
    • If the source string is longer than the destination buffer then strncpy won't null-terminate the destination string. You must not treat the destination buffer as a string until after it is null-terminated.

    Comment

    • boba
      New Member
      • Feb 2010
      • 4

      #3
      thx donbock for the quick reply.

      What if I am not sure what my destination and/or source buffer length is?

      also you said that "If the source string is longer than the destination buffer then strncpy won't null-terminate the destination string. You must not treat the destination buffer as a string until after it is null-terminated"

      So in that case what will happen if it does not null-terminate my string? will I have a buffer overflow?
      and what if my source string is shorter than my destination one, what will happen in that case??

      thx again

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Suppose the source string is
        char src[] = "This is a test";

        If the size of the destination buffer is 100 characters, then the source string (with terminating null) would be written to the first 15 characters of the buffer. The remaining 85 characters would be unchanged.

        If the size of the destination buffer is 15 characters (the value returned by sizeof(src)), the source string (with terminating null) would be written into those 15 characters.

        If the size of the destination buffer is 14 characters (the value returned by strlen(src)), the source string (not including terminating null) would be written into those 14 characters.

        If the size of the destination buffer is 3 characters, then the characters 'Thi' (without any terminating null) would be written into those 3 characters.

        Comment

        • boba
          New Member
          • Feb 2010
          • 4

          #5
          thx a lot Donbock, I really appreciate it

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You should be allocating the destination dynamically based on the data being copied.

            That is, do a strlen() of your data. Then allocate a buffer equal to the length from strlen() + 1 byte for a null terminator. Then do your strcpy().

            free() the buffer at the end of the program or when you are done with it.

            Comment

            • boba
              New Member
              • Feb 2010
              • 4

              #7
              thanks for the tip...

              Comment

              Working...