I am writing the game of life code. Now this is running for smaller size of matrices, but I need this to run for big matrices. So I need to deallocate the memory somehow. I tried it with the //deallocate part at the end of calculate(). But now its showing up an error.
Could you please deallocate the memory somehow so that it run for big matrices (like 10k * 10k with 10k iterations)?
#include<stdio.h>
#include<stdlib.h>
#include <malloc.h>
#include <math.h>
#include <sys/time.h>
#define ALIVE 1
#define DEAD 0
int **array = NULL;
int **history = NULL;
int HEIGHT;
int WIDTH;
double gettime(void) {
struct timeval tval;
gettimeofday(&tval, NULL);
return( (double)tval.tv_sec + (double)tval.tv_usec/1000000.0 );
}
int **allocarray(int P, int Q) {
int i;
int *p, **a;
p = (int *)malloc(P*Q*sizeof(int));
a = (int **)malloc(P*sizeof(int*));
if (p == NULL || a == NULL)
printf("Error allocating memory\n");
/* for row major storage */
for (i = 0; i < P; i++)
a[i] = &p[i*Q];
return a;
}
void initarray() {
int i,j;
for (i=1; i<HEIGHT-1; i++){
for (j=1; j<WIDTH-1; j++){
if(drand48()>0.5){
array[i][j] = 1;
}
else{
array[i][j] = 0;
}
}
}
}
void printTable(int **table) {
int height, width;
for (height = 1; height < HEIGHT-1; height++) {
for (width = 1; width < WIDTH-1; width++) {
if (table[height][width] == ALIVE) {
printf(" X ");
} else {
printf(" 0 ");
}
}
printf("\n");
}
printf("\n");
}
//to clear up everything
void clearTable() {
int height, width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
array[height][width] = DEAD;
}
}
}
int getNeighborValue(int **table, int row, int col) {
if (table[row][col] == ALIVE)
{
return 1;
} else {
return 0;
}
}
int getNeighborCount(int **table, int row, int col) {
int neighbor = 0;
neighbor += getNeighborValue(table, row - 1, col - 1); //top left
neighbor += getNeighborValue(table, row - 1, col); //top
neighbor += getNeighborValue(table, row - 1, col + 1); //top right
neighbor += getNeighborValue(table, row, col - 1); //left
neighbor += getNeighborValue(table, row, col + 1); //right
neighbor += getNeighborValue(table, row + 1, col - 1); //below left
neighbor += getNeighborValue(table, row + 1, col); // below
neighbor += getNeighborValue(table, row + 1, col + 1); //below right
return neighbor;
}
void calculate() {
int **tableB = NULL;
int neighbor, height, width;
tableB = allocarray(HEIGHT, WIDTH);
for (height = 1; height < (HEIGHT-1); height++) {
for (width = 1; width < (WIDTH-1); width++) {
neighbor = getNeighborCount(array, height, width);
if (neighbor==3) {
tableB[height][width] = ALIVE;
} else if (neighbor == 2 && array[height][width] == ALIVE) {
tableB[height][width] = ALIVE;
} else {
tableB[height][width] = DEAD;
}
}
}
for (height = 1; height < (HEIGHT-1); height++) {
for (width = 1; width < (WIDTH-1); width++) {
array[height][width] = tableB[height][width];
}
}
for (height = 1; height < (HEIGHT-1); height++) {
for (width = 1; width < (WIDTH-1); width++) {
free(tableB [height][width]);
free(tableB);
}
}
}
int matchArray(void){
int count = 0;
int height,width,flag=0;
for(height = 0; height<HEIGHT; height++){
for(width = 0; width<WIDTH; width++){
if(array[height][width] != history[height][width]){
count++;
}
}
}
if(count>0){
flag = 1;
}
return flag;
}
int main(int argc, char *argv[]) {
int generation = 0;
int i;
double starttime, endtime, time;
int height,width;
int flag = 0;
if (argc!=3)
printf("You need to enter the size of the matrix and the number of generations in the comment line arguement\n");
else
{
printf("The matrix size given is:%s * %s\n",argv[1], argv[1]);
HEIGHT = atoi(argv[1])+2;
WIDTH = HEIGHT;
array = allocarray(HEIGHT, WIDTH);
history = allocarray(HEIGHT, WIDTH);
clearTable();
initarray();
//printTable(array);
//printf("Test data as Generation 0\n");
//printf("-----------\n");
starttime= gettime();
//printf("Starttime= %f", starttime);
for(i=0; i<(atoi(argv[2])); i++)
{
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
history[height][width] = array[height][width];
}
}
calculate();
flag = matchArray();
//printf("The flag value is:%d\n",flag);
if(flag == 0){
break;
}
//printTable(array);
//printf("\n======================================\n");
//printTable(history);
//printf("Generation %d\n", ++generation);
}
endtime= gettime();
//printf("Endtime= %f", endtime);
time= (endtime-starttime);
printf("Time taken= %1f\n", time);
}
return 0;
}