Thread: strdup and malloc

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    38

    strdup and malloc

    hello,

    i was looking at the code online for strdup and wanted to ask a question regarding its implementation w.r.t malloc()

    Code:
    strdup
    merely copies the string given by its argument into a safe place, obtained by a call on
    malloc
    :
    char *strdup(char *s) /* make a duplicate of s */ { char *p; p = (char *) malloc(strlen(s)+1); /* +1 for '\0' */ if (p != NULL) strcpy(p, s); return p; }
    I wanted to know why is there a need to call malloc on the variable p? wont this function still work without the malloc?

    hope to hear from you soon.

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    Code:
       char *strdup(char *s)   /* make a duplicate of s */   {       char *p;       p = (char *) malloc(strlen(s)+1); /* +1 for '\0' */       if (p != NULL)           strcpy(p, s);       return p;   }

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    strcpy does not allocate memory, it just copies what it's told to, where it's told to copy it. Malloc actually allocates memory so that strcpy has somewhere to copy TO.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    So, if we do not malloc, it will not copy from s to p?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by ueg1990 View Post
    So, if we do not malloc, it will not copy from s to p?
    If you DO NOT do malloc inside the strdup function then there is nowhere legal/valid/safe to copy the string.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    oh ok...thnx!!

    btw nice quote!!!

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Without the malloc call, the strcpy will be copying from source string s to an uninitialized pointer p. Being uninitialized, p could be pointing anywhere, most likely somewhere that will cause a segfault if you attempt to write to it. A segfault would be a good option as you'd know you were doing something wrong immediately. A bad option would be the strcpy would silently trash some other critical data without you knowing anything was amiss leaving your program to merrily go on about its business without apparent fault.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strdup
    By darren78 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2010, 08:29 AM
  2. Strdup
    By Ducky in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2010, 12:52 PM
  3. Regarding 'strdup()'
    By subhashish1213 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2009, 09:18 AM
  4. strdup
    By hankspears in forum C Programming
    Replies: 4
    Last Post: 05-09-2002, 02:02 PM
  5. function: strdup()
    By cjtotheg in forum C Programming
    Replies: 3
    Last Post: 02-02-2002, 10:08 AM