Reputation: 6766
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?
Upvotes: 0
Views: 949
Reputation: 503
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.
Upvotes: 0
Reputation: 361546
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);
}
Upvotes: 4