Thread: a quick question about strcpy

  1. #1
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85

    a quick question about strcpy

    is it legal to pass an address adjusted char pointer, to strcpy as its second arguement? as in:


    strcpy (end, a_line + beginning_length)

    I want the end of the string to copy from the beginning length to the null sentinal of a_line. Both end and a_line are char pointers.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Absolutely!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    thanks, thats what i thought

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    while were on the topic, is this legal:
    Code:
    strcpy(str, str+1)
    If I'm correct in assuming most strcpy implementations simply do something like:
    Code:
    while((*dest++ = *src++) != '\0')
    I'd think it would be safe, though the man page doesn't really go into much about the behaviour of editing strings concurrently in the str functions.
    Last edited by @nthony; 08-26-2007 at 09:51 PM. Reason: (dest, src)

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Does the man page mention anything about the fact that the strings may not overlap?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by @nthony View Post
    while were on the topic, is this legal:
    Code:
    strcpy(str+1, str)
    If I'm correct in assuming most strcpy implementations simply do something like:
    Code:
    while((*dest++ = *src++) != '\0')
    I'd think it would be safe, though the man page doesn't really go into much about the behaviour of editing strings concurrently in the str functions.
    That's exactly why it WON'T work. If strcpy() is implemented as you describe (and it usually is), the result is an infinite loop that eventually crashes.

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    woops, indeed it does, I must have been thinking of another man page. Yep, it clearly says undefined behaviour:
    If copying takes place between objects that overlap, the behavior is undefined...
    Character movement is performed differently in different implementations. Thus, overlapping moves may yield surprises.
    This issue is aligned with the ISO C standard; this does not affect compatibility with XPG3 applications. Reliable error detection by this function was never guaranteed.
    nm, I guess I was thinking of something else, I'll post again if I remember the function, I just remember the arguments were overlapping strings.

    p.s. I wonder what MingW's implementation is for this, because I think I've gotten away with this numerous times in the past.

    edit: sorry brewbuck, I meant "strcpy(str, str+1)", backward src/dest args in C get me all the time... :S
    Last edited by @nthony; 08-26-2007 at 09:52 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by @nthony View Post
    woops, indeed it does, I must have been thinking of another man page. Yep, it clearly says undefined behaviour:
    nm, I guess I was thinking of something else, I'll post again if I remember the function, I just remember the arguments were overlapping strings.

    p.s. I wonder what MingW's implementation is for this, because I think I've gotten away with this numerous times in the past.
    memmove?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by @nthony View Post
    edit: sorry brewbuck, I meant "strcpy(str, str+1)", backward src/dest args in C get me all the time... :S
    It's not backwards. Remember it this way:

    Code:
    a = b; /* The data flows leftward */
    strcpy(dst, src); /* Again, the data flows leftward. */

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dave_Sinkula View Post
    memmove?
    memmove() works fine for overlapping regions. It's memcpy() which can't be trusted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM