What is the rationale for not including strdup in the C Standard

Most C programmers are familiar with the strdup function. Many of them will take it for granted, yet it is not part of the C Standard (neither C89, C99 nor C11). It is part of POSIX and may not be available on all environments. Indeed Microsoft insisted on renaming it _strdup, adding to confusion.

It is rather easy to define it this way (in C):

#include <string.h>

char *strdup(const char *s) {
    size_t size = strlen(s) + 1;
    char *p = malloc(size);
    if (p) {
        memcpy(p, s, size);
    }
    return p;
}

But even savvy programmers can easily get it wrong.

Furthermore, redefining the function only on systems that do not have it proves a bit complicated as explained here: strdup() function

Why not include such useful widely supported functions in revised editions of the C Standard? A lot of new functions have been included the C standard library in C99, what is the rationale for not including strdup?


Source: c#

Leave a Reply