【发布时间】:2013-03-08 23:22:28
【问题描述】:
我不明白为什么这些指针值似乎是正确的,但我试图从中得到的值却不是。 (我很久以前就学过 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;
}
输出的是指向 dirent 结构的指针值、该结构中的文件名和文件序列号。然后它尝试将指向该 direct 的指针转换为 (void*) 并将其存储在临时堆栈中。
稍后,我尝试迭代堆栈(以及数组)中的值,将其转换回 dirent,然后显示相同的信息。
这有时有效,有时无效。修剪后的输出示例:
...
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领域有一段时间了。请放轻松。)
【问题讨论】:
-
“我很久以前学过 C 语言,最近我想重新学习它以获得乐趣” - 有很多事情比用 C 语言编程更有趣C.
-
@LihO 没错,但你只能吃这么多。
-
如果你知道你要存储指向
struct dirent的指针,那你为什么把它声明为void* value[Q_STACK_MAX];呢?我的意思是为什么void*? -
readdir返回的指针可能是静态分配的,后续调用readdir会覆盖其内容。可能是这样。如果在将数据压入堆栈之前复制数据会发生什么? -
@LihO 最后它不会只是 struct dirent,这只是一个简化的例子