arrays - C matrix function segmentation fault -



arrays - C matrix function segmentation fault -

i have piece of code gives me segmentation fault when function loadrow(tmppop,bestof4route,k,n); called 5th time. in particular, function called correctly @ first loop of fro (when p=3). it' ok every value of k. don't understand why, sec time execute loop (p=7), first time called (k=0), homecoming segmentation f when seek access matric tmppop.

randperm(popsize,randomorder); for(p = 3;p<popsize;p+=4) { load_rtes(rtes,pop,randomorder,n,p); load_dists(dists,totaldist,randomorder,p); find_min(dists, 4,&m,&idx); for(j=0;j<n;j++) bestof4route[j]=rtes[j][idx]; x = rand_range(1,5); y = rand_range(1,5); for(k =0;k<4;k++) //mutate best 3 new routes { loadrow(tmppop,bestof4route,k,n); switch(k) { case 1: //flip flipmutation(tmppop,k,x,y); break; case 2: //swap swapmutation(tmppop,k,x,y); break; case 3: //slide slidemutation(tmppop,k,x,y); break; } } loadnewpop(newpop,tmppop,p,n); }

the function is:

void loadrow(int **mat,int *array,int k,int size) { int j; for(j=0;j<size;j++) { mat[j][k] = array[j]; } }

the parameters are:

popsize = 16

n= 8 // create 4 x n matrix tmppop = (int**)malloc(n * sizeof(int*)); if(tmppop==null) homecoming 1; (i = 0; < n; i++) { tmppop[i] = (int*)malloc(4 * sizeof(int)); if(tmppop[i]==null) homecoming 1; } // creates array of n bestof4route = (int*)malloc(n * sizeof(int)); if(bestof4route==null) homecoming 1; clear_array(bestof4route,n);

and debug result:

00401865 loadrow(mat=0x3e1438, array=0x3e1698, k=0, size=8)

void load_rtes(int **rtes,int **pop,int *randomorder, int n,int p) { int i,j,r; for(i=p-3;i<=p;i++) { //thakes element of randomorder , utilize index pop row r=randomorder[i]; // re-create pop row in rtes for(j=0;j<n;j++) { rtes[j][i]=pop[j][r]; } } } void randperm(int n,int *perm) { int i, j, t; for(i=0; i<n; i++) perm[i] = i; for(i=0; i<n; i++) { j = rand()%(n-i)+i; t = perm[j]; perm[j] = perm[i]; perm[i] = t; } }

a segfault means accessed memory part weren't allowed access. can happen quite when doing pointer arithmetic wrong. so, although can't tell problem lies, suggest utilize debugger or printf() statements @ k, n , j see if exceed bounds of matrix / array. also, you'll need create sure allocated right amount of memory.

c arrays dynamic matrix segmentation-fault

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -