[英]Error allocating memory of unsigned longs in C
我正在編寫一個需要使用一些非常大的數組的C程序。 我通過調用為數組分配內存:
unsigned long *visited = declareArrayofLongs(my_pow(2, d));
declareArrayofLongs()
函數為:
unsigned long int* declareArrayofLongs(unsigned long int length)
{
unsigned long int *myarray = (unsigned long int*) malloc(length*sizeof(unsigned long int));
if (myarray==NULL)
printf("Error allocating memory for unsigned longs!\n");
unsigned long int i;
for(i=0; i<length; i++)
myarray[i] = 0;
return myarray;
}
而my_pow()
是
unsigned long int my_pow(int base, int exp)
{
unsigned long int pow=1;
int i=0;
for(i=0; i<exp; i++){
pow = (unsigned long int)base*pow;
}
return pow;
}
當程序運行並達到d = 29左右時,出現“為無符號長型分配內存錯誤!”。 我的系統上有16gb的RAM。 我不應該能夠處理此數量的分配嗎?
一次僅聲明一個visited
數組。 重新分配之前,我有一個free(visited)
電話。
您沒有顯示my_pow。 由於您的代碼不起作用,是什么讓您認為它像您想的那樣工作?
您可能正在運行32位代碼,在這種情況下,無論有多少RAM,都不會超過3GB。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.