Quantcast
Channel: BSDnexus
Viewing all articles
Browse latest Browse all 17

implementing my own strdup

$
0
0

While chatting on IRC last night I entered conversation about strdup(), a C function I had never used or encountered. I was then challenged to write my own. Checking the man page had me thinking about a for loop to iterate the data from one place to another, however, the genious Maxlor then pointed me to memcpy() (another function I had never used.)

Two minutes later I had my own version of strdup.

char * mystrdup( const char * string )
{
    char * p;
    if ( (p = malloc(strlen(string)) ) == NULL )
        return NULL;

    return memcpy(p, string, strlen(string));
}

Quite interesting to think through how some functions you use (or don’t use) are implemented.


Viewing all articles
Browse latest Browse all 17

Latest Images

Trending Articles





Latest Images