Thread: strcpy and strncpy

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    strcpy and strncpy

    The strcpy function and strncpy functions are used to copy the source string to the target string (one copies the entire string and the other copies the characters upto certain lenght as specified in the arguments)

    In one of the places it is quoted as

    "If src and dst are overlapping string, the results of strcpy and strncpy are undefined"

    What do they mean by overlapping? I mean how can two strings overlap when they are two different addresses? Is there something wrong with this statement or i am mistaking it for something else?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    For example:

    Code:
    char buf[ 1024 ] = "foobarbaz";
    strcpy( buf + 3, buf ); // undefined
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Okay but then it is an error on the user part because he is trying to copy one string to another location which is already having characters.

    So it is to avoid such confusion that the result becomes undefined. But then the buf in this case wont get corrupted and will continue to hold whatever was contained in it initally. Is it so?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by roaan View Post
    Okay but then it is an error on the user part because he is trying to copy one string to another location which is already having characters.
    That is not at all what the problem is. By definition, there are always characters in the destination string already when you start. (They may be the old value, they may be garbage, they may be \0 characters, but they are there.) The problem is that the source and destination overlap; the first character of the destination is in the same place as the fourth character of the source. If you copy character by character, then that fourth character is overwritten before it can be copied....

    And undefined means just that -- you cannot predict what will happen after that statement executes.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by roaan View Post
    Okay but then it is an error on the user part because he is trying to copy one string to another location which is already having characters.

    So it is to avoid such confusion that the result becomes undefined. But then the buf in this case wont get corrupted and will continue to hold whatever was contained in it initally. Is it so?
    Right so if you need to copy within the string you just need a set of algorithms that guarantee the direction of copying (eg: copy_forward, copy_backward).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    @all

    That makes sense and a lot clearer to me now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new c users question
    By timS in forum C Programming
    Replies: 11
    Last Post: 10-05-2004, 06:13 PM
  2. Memory Access Error when using Strcpy()
    By fgs3124 in forum C Programming
    Replies: 2
    Last Post: 03-15-2002, 03:07 PM