Declaring two large 2d arrays gives segmentation fault.
- by pfdevil
Hello, i'm trying to declare and allocate memory for two 2d-arrays. However when trying to assign values to itemFeatureQ[39][16816] I get a segmentation vault. I can't understand it since I have 2GB of RAM and only using 19MB on the heap. Here is the code;
double** reserveMemory(int rows, int columns)
{
double **array;
int i;
array = (double**) malloc(rows * sizeof(double *));
if(array == NULL)
{
fprintf(stderr, "out of memory\n");
return NULL;
}
for(i = 0; i < rows; i++)
{
array[i] = (double*) malloc(columns * sizeof(double *));
if(array == NULL)
{
fprintf(stderr, "out of memory\n");
return NULL;
}
}
return array;
}
void populateUserFeatureP(double **userFeatureP)
{
int x,y;
for(x = 0; x < CUSTOMERS; x++)
{
for(y = 0; y < FEATURES; y++)
{
userFeatureP[x][y] = 0;
}
}
}
void populateItemFeatureQ(double **itemFeatureQ)
{
int x,y;
for(x = 0; x < FEATURES; x++)
{
for(y = 0; y < MOVIES; y++)
{
printf("(%d,%d)\n", x, y);
itemFeatureQ[x][y] = 0;
}
}
}
int main(int argc, char *argv[]){
double **userFeatureP = reserveMemory(480189, 40);
double **itemFeatureQ = reserveMemory(40, 17770);
populateItemFeatureQ(itemFeatureQ);
populateUserFeatureP(userFeatureP);
return 0;
}