lundi 22 décembre 2014

Error allocating memory of unsigned longs in C


I am writing a program that needs to use some pretty large arrays. I allocate memory for my array by calling:



unsigned long *visited = declareArrayofLongs(my_pow(2, d));


The declareArrayofLongs function is:



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;
}


And my_pow(2, d) returns 2^d as an unsigned long. When the program runs and gets up to around d=29, I get "Error allocating memory for unsigned longs!". I have 16gb of RAM on my system. Shouldn't I be able to handle this amount of allocation?


Only one visited array is declared at a time. I have a free(visited) call before re-allocating.





Aucun commentaire:

Enregistrer un commentaire