Thread: strcpy, strdup

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    strcpy, strdup

    hi friends,


    what is the diff. between strcpy() and strdup(). Both duplicate string right?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No they don't
    strcpy is a standard function which copies a \0 terminated array of chars from one place to another (so long as they don't overlap)
    strdup is a non-standard function which MAY allocate some memory and copy a string into that memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Hmmm. Salem, can you give an example of when strdup DOESN'T return a new memeory location? I did some looking (honestly not long) on Google and couldn't come up with anything.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I am guessing this is what Salem meant...

    strdup returns a pointer to the new duplicate string, or NULL if there was not enough memory. In general, strdup makes a call to malloc to allocate space for the new string - if malloc can't get enough space, neither can strdup, so in this case no duplicate string. Since strdup requests space from malloc it may or may not allocate some memory and copy the string into the new space, depending on what malloc returns.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    More specifically, an ANSI-C implementation isn't required to implement strdup() AT ALL.

    Even if you have two systems providing strdup(), you have to read the manual for both to make sure it's what you want.
    That's why it's a non-standard function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    So, if then the lib that one is using has strdup, does it ALWAYS allocate memory, or are there any that would keep a static buffer such that one would need to do his/her own malloc and then strcpy?

    Granted, there is no requirement for strdup in C, however, I don't know of a lib that doesn't offer it. But, that wasn't my point. I was only wondering if there was some other, to use the term loosely, non-standard way of implementing strdup?

    The reason that I even asked is because you said it "MAY allocate" memmory. . . which made me think that you already knew of some other way strdup was implemented.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You mean like using two standard functions - malloc and strcpy to implement it yourself and be sure that the result will be portable?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Where is strcpy() defined? (Can't find in string.h ect)
    By Zero_Point in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 05:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  5. strcpy and strdup?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-26-2001, 06:46 AM