I am writing a C 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()
is
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;
}
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.