Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

I know there are already plenty of comments about the problems but might as well give mine too...

In one sentence: this guy managed to reinvent a variant of strcat and get it completely wrong.

Here's a somewhat better version:

    char *combine(char *s, char *t) {
        size_t m = strlen(s), n = strlen(t);
        char *ret = malloc(m + n + 1);
        if(ret) {
            memcpy(ret, s, m);
            strcpy(ret + m, t);
        }
        return ret;
    }


You already know the length of t. strcpy is inefficient when another memcpy will do.


Minor bug:

If s and t are very long and overlapping, m + n could wrap around.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: