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:

唯一会影响path的是opendir()这里。它有我没有看到的副作用吗?还是有其他事情在工作?

4

1 回答 1

3

不允许更改;的定义opendir()是:

DIR *opendir(const char *dirname);

constopendir()并没有改变它。

我想知道您path是否是指向已释放内存的指针?在这种情况下,内存可能已分配到opendir()并且您看到了更改,因为您使用了指向您不应该查看的内存的悬空指针?

于 2012-03-16T02:21:50.797 回答