So basically I want to find the number of Gb free on my computer (just the whole Gbs, ignore the fraction Gb) using malloc() function. Here us the code that I use:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argv, char **argc) {
unsigned long size = 0;
void *part[1000];
int i = 0;
part[size] = (void *)malloc(1024*1024*1024);
if(part[size] == 0)
printf("The computer has less than 1 Gb of free memory\n");
else {
while((part[size] != NULL) && (size<1000)) {
size++;
part[size] = (void *)malloc(1024 *1024 *1024);
}
while(i <= size) {
free(part[size]);
}
printf("The computer has %luGb of free memory\n", size);
}
return 0;
}
The result is segmentation fault(Core dumped), I don't really know why this happen and would be really appreciate if anyone can point out where I got wrong. Thanks :)