Hey guys, I was wondering if you had any idea about this. Basically I'm creating a program to model Diffusion Limited Aggregation and am having segmentation errors due to my array memory allocation. After loads of debugging I've narrowed it down to the while loop that I use for my random walk. This while loop basically runs through the array "grid" checking every element adjacent to the "particle". If the particle becomes adjacent to another particle in the grid, the value of the element in the array changes from 0 to 1, and the whole process begins again for a new particle. The model works fine for an array of dimension 10x10 and for 5 particle. However, when it gets to higher dimensions and more particles, aka the more steps in the while loop, it fails and gives me segmentation error. Here's the source code, any help appreciated.
Code:
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
double *lAlloc (int size);
int stick(int **grid, int size, int noPars);
double seedLoc(int size, int *centrex, int *centrey);
int main()
{
int size = 10000;
int **grid, i, l, n;
grid = (int **)malloc(size * sizeof(int *) );
for ( i = 0 ; i < size ; ++i )
{
grid[i] = (int *) malloc( size * sizeof(int *) );
}
if(grid == NULL)
{
puts("........ dun ........ed up");
}
else
{stick(grid, 100, 5);
}
for(l = 0; l<100; l++)
{
for (n = 0; n<100; n++) {
if(grid[l][n] == 1)
{
printf("X = %d, Y = %d, VAL = %d\n",l,n, grid[l][n]);
}
}
}
}
double seedLoc(int size, int *centrex, int *centrey)
{
puts("got here 1");
*centrex = size/2;
*centrey = size/2;
}
int stick(int **grid, int size, int noPars)
{
int centrex, centrey, i, j, k, posX, posY;
int rnd, newX, newY, step;
srand((unsigned)time(NULL)); // To increase Pseudorandomness by using a dynamic seed.
seedLoc(size, ¢rex, ¢rey);
printf("Centrex, Centry: %d %d\n",centrex, centrey);
for (i=0; i < size; i++)
{
for (j=0; j<size; j++)
{
grid[i][j] = 0;
}
}
grid[centrex][centrey] = 1;
printf("Seed Value %d\n", grid[centrex][centrey]);
//choosing random particle firing location on edge of grid
if(((rand() % 4)+1) == 1)
{
posX = 0;
posY = rand() % size;
}
else if(((rand() % 4)+1) == 2)
{
posY = 0;
posX = rand() % size;
}
else if(((rand() % 4)+1) == 3)
{
posY = size;
posX = rand() % size;
}
else
{
posY = rand() % size;
posX = size;
}
printf("posY %d,posX %d\n", posX, posY);
for (k = 0; k < noPars; k++)
{
printf("k = %d\n", k);
newX = posX;
newY = posY;
int l, n;
int m = 0;
while (
((grid[newX + 1][newY] || grid[newX - 1][newY] || grid[newX][newY - 1] || grid[newX][newY + 1]) != 1)
)
{
//printf("newX: %d, newY: %d, rnd = %d, size = %d, Step Number = %d\n", newX, newY, rnd, size, m);
rnd = rand() % 4 + 1;
step = 1;
if(rnd == 1)
{
if(newX == size)
{
step = step * (-1);
}
newX = newX + step;
}
else if(rnd == 2)
{
newX = newX - 1;
newX = abs(newX);
}
else if(rnd == 3)
{
if(newY == size)
{
step = step * (-1);
}
newY = newY + step;
}
else
{
newY = newY - 1;
newY = abs(newY);
}
m = m + 1;
}
grid[newX][newY] = 1;
printf(",mX = %d, Y = %d, VAl = %d\n", newX, newY, grid[newX][newY]);
}
}