Question

I use the following code to find the disk usage of my /

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

The perror keeps on printing "Bad Address" and a random number for size.

Bad address

3264987920

PS:I tried sudo ./a.out,statfs("a.out",stat)

What may be the issue?

Était-ce utile?

La solution

You've declared a pointer to a statfs struct but don't actually have space allocated for such a struct. The pointer points off into nowhereland. It's uninitialized, it doesn't point anywhere legal.

struct statfs stat;

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

Autres conseils

You have used statfs *stat with no memory allocation hence wild pointer usage it may point to anywhere ( illegal memory address) Either Initialise it with valid memory or use variable and pass its reference.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top