2

使用中にいくつかの奇妙な結果を受け取りましたopendir()

int dtw(char *path) {

    struct stat statbuf;

    ...

    else if (S_ISDIR(statbuf.st_mode)) {
            printf("Path is: %s\n", path);

            struct dirent *dirent;
            DIR *dirp;

            if ((dirp = opendir(path)) == NULL) {
                puts("Can't open directory.");
                return -1;
            }

            printf("Path is: %s\n", path);
    }

    ...
}

結果:

Path is: /home/.../etc
Path is:

影響するのpathopendir()ここだけです。私が見ない副作用はありますか?それとも他に何か仕事がありますか?

4

1 に答える 1

3

変更は許可されていません。の定義opendir()は次のとおりです。

DIR *opendir(const char *dirname);

そして、const言うopendir()ことはそれを変えませんでした。

あなたpathは解放されたメモリへのポインタなのだろうか?その場合、メモリが割り当てられている可能性があり、opendir()見るべきではないメモリへのダングリングポインタを使用しているため、変更が表示されていますか?

于 2012-03-16T02:21:50.797 に答える