Web Log

Weblog

All | Cycling | Gadgets | General | Installers | Licensing and Registration | Music | Programming | Science and Nature
« Previous day (Sep 19, 2005) | Main | Next day (Sep 21, 2005) »
20050920 Tuesday September 20, 2005

Stupid errors I have made, #1 of several thousand

Today's stupid programming error involves recursive directory handing in C. I was doing this, and wondering why it didn't work:

void
foo(char *path)
{
        /* stuff */
        if(condition) {
               foo(dirname(path));
               /* other stuff */
        }
        /* yet more stuff */
}

Can you see the problem? Yup, that's right, dirname() modifies the string pointed at by its argument, so when you pop up a level, your path is still trimmed. The solution is to strdup(path) before the recursive call, and free() the copy afterwards.

D'oh!

Tag:

( Sep 20 2005, 07:08:29 PM PDT ) Permalink Comments [2]

Calendar

RSS Feeds

Search

Links

Navigation

Referers