cstringmemorymallocstrdup

Should I free strdup pointer after basename/dirname in C?


I want to use POSIX's basename function (as opposed to GNU's).

From the man page:

Both dirname() and basename() may modify the contents of path, so it may be desirable to pass a copy when calling one of these functions.

These functions may return pointers to statically allocated memory which may be overwritten by subsequent calls. Alternatively, they may return a pointer to some part of path, so that the string referred to by path should not be modified or freed until the pointer returned by the function is no longer required.

It also says:

RETURN VALUE
Both dirname() and basename() return pointers to null-terminated strings. (Do not pass these pointers to free(3).)

So the example suggests something like:

EXAMPLE

       char *dirc, *basec, *bname, *dname;
       char *path = "/etc/passwd";

       dirc = strdup(path);
       basec = strdup(path);
       dname = dirname(dirc);
       bname = basename(basec);
       printf("dirname=%s, basename=%s\n", dname, bname);

The strdup(strndup) man page says:

Memory for the new string is obtained with malloc(3), and can be freed with free(3).

So the question is: Should I free dirc and basec (as per strdup) or not (as per basename)?


Solution

  • dname and bname could be using parts of dirc and basec respectively, so it's not safe to free dirc or basec.

    I'd do an strdup of the results from the dirname and basename calls. Then it is safe to freedirc and basec, and you KNOW you have to free your copies.

    (yes it's messy, but I think it's cleaner than remembering you can't free dirc yet because dname may or may not be using it...)