文件系统统计信息

时间:2013-04-17 01:40:59

标签: c diskusage

我使用以下代码查找/

的磁盘使用情况
int main()
{
    struct statfs *stat;
    statfs64("/tmp",stat);
    perror("");
    printf("%lu \n",stat->f_bfree*stat->f_bsize);
    return 0;
}

perror继续打印“Bad Address”和一个随机数字。

Bad address

3264987920

PS:我尝试了sudo ./a.outstatfs("a.out",stat)

可能是什么问题?

2 个答案:

答案 0 :(得分:4)

您已经声明了一个指向statfs结构的指针,但实际上没有为这样的结构分配空间。指针指向无处。它没有初始化,它没有任何合法性。

struct statfs stat;

if (statfs64("/tmp", &stat) == -1) {
    perror("statfs64");
}
else {
    printf("%lu\n", stat.f_bfree * stat.f_bsize);
}

答案 1 :(得分:0)

你已经使用了statfs * stat而没有内存分配因此可能指向任何地方的非常指针用户(非法内存地址) 使用有效内存初始化它或使用变量并传递其引用。

相关问题