strdup() function

前端 未结 7 1308
孤城傲影
孤城傲影 2020-12-02 00:01

I recently became aware that the strdup() function I\'ve enjoyed using so much on OS X is not part of ANSI C, but part of POSIX. I don\'t want to rewrite all my

7条回答
  •  既然无缘
    2020-12-02 00:13

    If anyone else reads this: Don't use a platform's strdup() even if available, and don't waste time/effort with autoconf/automake just to use it. Seriously, how hard is this:

    char* mystrdup(const char* str)
    {
     return strcpy(malloc( strlen(str) + 1),str);
    }
    

    Does this really warrant #ifdefs? Compiler checks? K.I.S.S.

提交回复
热议问题