1

これらのポインタ値が正しいように見える理由はわかりませんが、それらから取得しようとしている値は正しくありません。(私はずっと前にCを勉強しました、そして私は最近楽しみのためにそれに戻ろうとしています)。

これが私が直面している問題の実際の例ですが、私が間違っていることはわかりません-ポインタを間違ってキャストしているのか、メモリを正しく管理していないのか、または他の何かです。

#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>

#define Q_STACK_MAX 100

typedef struct
{
    void* value[Q_STACK_MAX];
    int top;
} Stack;


void push(Stack* S, void* val)
{
    S->value[S->top] = val; 
    (S->top)++;
}

void* pop(Stack* S)
{
    (S->top)--;
    return (S->value[S->top]);
}

void init(Stack* S)
{
    S->top = 0;
}

int full(Stack* S)
{
    return (S->top >= Q_STACK_MAX);
}


void recursive_dir(char* dir, char* search, Stack* S)
{
    DIR* dp;
    struct dirent* entry;
    struct stat statbuf;
    if((dp = opendir(dir)) == NULL) {
        fprintf(stderr, "error: %s\n", dir);
        return;
    }

    chdir(dir);
    while((entry = readdir(dp)) != NULL) {
        lstat(entry->d_name, &statbuf);

        if(S_ISDIR(statbuf.st_mode)) {
            if(strcmp(".", entry->d_name) == 0 ||
                strcmp("..", entry->d_name) == 0)
                    continue;

            recursive_dir(entry->d_name, search, S);
        } else {
            if( strstr(entry->d_name, search) != NULL ) {
                if(!full(S)) push(S, (void *)entry);

                printf("%p\n", entry);
                printf("%s [%x]\n", entry->d_name, entry->d_ino);
            } 
        }
    }
    chdir("..");
    closedir(dp);
}

int main() {
    Stack S;
    init(&S);
    int i;

    printf("\n");
    recursive_dir("/etc/", "conf", &S);
    printf("\n------------------------------\n");

    int top = S.top;
    struct dirent* entry;
    for (i=0; i<top; i++)
    {
        //struct dirent* entry = (struct dirent*)pop(&S);
        entry = (struct dirent*)S.value[i];

        printf("%p\n", entry);
        printf("%s [%x]\n", entry->d_name, entry->d_ino); 
    }

    printf("\n");

    return 0;
}

これが出力するのは、異なる構造へのポインター値、その構造内にあるファイル名、およびファイルのシリアル番号です。次に、その直接へのポインタを(void *)にキャストし、その場しのぎのスタックに格納しようとします。

後で、スタック内の値(配列も同様)を反復処理し、それをダイレントにキャストして戻し、同じ情報を表示しようとします。

これは時々機能し、他の時には機能しません。トリミングされた出力例:

...
0x7ff831806360
httpd-multilang-errordoc.conf [2cb6e07]
0x7ff831806398
httpd-ssl.conf [2cb6e08]
0x7ff8318063c0
httpd-userdir.conf [2cb6e09]
0x7ff8318063ec
httpd-vhosts.conf [2cb6e0a]
0x7ff831805250
httpd.conf [2cb6e0b]
0x7ff831805274
httpd.conf~previous [187a3]
0x7ff831807230
httpd-autoindex.conf [2cb6e10]
0x7ff831807260
httpd-dav.conf [2cb6e11]
0x7ff831807288
httpd-default.conf [2cb6e12]
0x7ff8318072b4
httpd-info.conf [2cb6e13]
...
--------------------------
...
0x7ff831807360
httpd-multilang-errordoc.conf [2cb6e17]
0x7ff831807398
httpd-ssl.conf [2cb6e18]
0x7ff8318073c0
httpd-userdir.conf [2cb6e19]
0x7ff8318073ec
httpd-vhosts.conf [2cb6e1a]
0x7ff831806250
 [0]
0x7ff831806230
320.whatis [2cb6ff6]
0x7ff8318042bc
asl.conf [2cb7d07]
0x7ff83180437c
autofs.conf [2cb6f5f]
0x7ff831805250
 [0]
0x7ff831805274
??$ [61666564]
0x7ff8318052e0
_response [6e7261]
...

すべてが正しいように見える場合もあれば、ファイルハンドルや名前が完全に破壊されている場合もあります。

一般的に、そのようなことを見ると、メモリを使って何かを正しく行っていないことが原因です。ガベージコレクションの世界に長く住んでいたので、それは驚くことではありませんが、理由や方法はわかりません。

どんな助けでもいただければ幸いです。

(私はかなりの年数の間コーディングをしてきました、私はちょうどかなり長い間Cアリーナから出ていました。私に気楽に行ってください。)

4

1 に答える 1

5

direntによって返される構造体は、readdir()繰り返しの呼び出しでメモリを再利用できます。それらを保存したい場合は、それらを動的オブジェクトにコピーするか、最初にオブジェクトを割り当てて を呼び出す必要がありますreaddir_r()

于 2013-03-08T23:33:07.927 に答える