I've defined a random function (int random(int sup, int seed)) which returns a value between 0 and sup-1.
I've defined a struct, point, of which pos_parents and population are 2-dimensional arrays.
The swap functions swap elements of the v array, which is a array of "indexes". All is done in order to sort out par_n members into pos_parents out of population members without sorting twice the same member.
This gives segmentation fault.
If I replace the variable r inside population[v[r]][j] with an explicit value, then it all functions. How is this possible? I've tried the random function and it doesn't seem to have any problem.
In addition, when segmentation fault occours, printf won't even activate for the first loop.
point population[pop_size][array_size];
point pos_parents[4*par_n][array_size];
int v[pop_size];
for (i=0; i<4*par_n;i++)
v[i]=i;
for(t=0;t<time_limit;t++) //The cycle of life
{
for(i=0;i<4*par_n;i++)
{
r=random(pop_size-i,i);
printf("%d\t",r);
for(j=0;j<array_size;j++)
{
pos_parents[i][j]=population[v[r]][j];
}
swap(&(v[r]),&(v[pop_size-1-i]));
}
When executing i type 3(route locations-array size), 8(pop_size), 1(time limit), 1 (par_n)
This is the entire code (less than 150 lines), always insert 1 to time_limit, because I haven't still completed the cycle. https://docs.google.com/open?id=0ByylOngTmkJddVZqbGs1cS1IZkE
P.S. I'm trying to write an evolutionary algorithm, for route optimization
v[]is less than4*par_n. So the first for-loop is overwriting something important in the stack frame. - Barmarpar_nmembers, why are you using4 * par_nfor the upper bounds on your arrays? That could be a major part of your trouble. Theprintf()typically won't send anything to the screen until a newline is printed, or until the print buffer is full. For debugging, either print tostderr, or print a newline (and optionally usefflush(0)to flush the output. I worry about you providing the seed to the random function each time. For the rest, please consider providing a SSCCE — a Short, Self-Contained, Correct (Compiling) Example that reproduces the problem. - Jonathan Leffler