因此,基本上我想使用malloc()函數在我的計算機上找到免費的Gb數(只是整個Gbs,忽略分數Gb)。這裏我們說我使用的代碼:使用malloc函數查找可用內存空間
#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;
}
結果是分段錯誤(核心轉儲),我真的不知道爲什麼出現這種情況,會很感激,如果任何人都可以指出我哪裏錯了。 謝謝:)
請注意,由於[內存過量使用](https://www.google.co.uk/search?q=overcommit+memory),此方法不會告訴您有多少可用內存(至少不是在Linux上) )。 –
沒有多少修復代碼可以使它做你想做的事情。什麼是真正的問題? –
可能你想要http:// stackoverflow。com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process – qwr