strdup() function

前端 未结 7 1305
孤城傲影
孤城傲影 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:35

    you could just use a macro like this, this way you can use the old name, but linker will see a different name;

    char *my_strdup(const char *s) {
        char *p = malloc(strlen(s) + 1);
        if(p) { strcpy(p, s); }
        return p;
    }
    
    /* this goes in whatever header defines my_strdup */
    char *my_strdup(const char *s);
    #define strdup(x) my_strdup(x)
    
    0 讨论(0)
提交回复
热议问题