recursion problem, getting "stack smashing detected", but i dont know why?? please ex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kamlesh1
    New Member
    • Oct 2017
    • 1

    recursion problem, getting "stack smashing detected", but i dont know why?? please ex

    i m actually using recursion to find the different ways to reach a point in a 2d array from a fixed given point. i have to choose a start and target point randomly, i can only move right and down(i m choosing the start and end point according to that) i know that at every step i have to cll the same function 2 times, one for right and another time for moving down. further, i have to print the path only if it has more than 1 prime number, hence i have created a isprime function as well.
    there is no error in my code, but i m getting "stack smashing detected." problem

    what i have done in my code

    1 i have made a n*n matrix with randomly filled values in it.
    2 i have chosen the start and the target/end point.
    3 i have made another matrix such that its start element is the start and the last element is the target/end
    4 i have made an array int path[pnt+1], where pnt = 0 initially.
    5 called a function move, with x,y(size of the array), array itself, pnt,i,j(i and j are the start index), i1,j1 (i1 and j1 are the target index), pnt, and finally path array

    here is my entire code with comments

    int prime(int a){
    int i=2,flag=0;
    for(i;i<a;i++){
    if(a%i==0){
    flag=1;
    break;
    }
    }
    if(flag==1)
    return 0;

    else
    return 1;
    }

    void move(int x,int y,int ar[x][y],int i,int j,int pnt,int path[pnt+1],int i1,int j1)
    {
    if(i==i1&&j==j1 ){
    //if start is same as target, it will check the prime in the path
    int pcnt=0;
    for(int a=0;a<pnt;a++){
    if(prime(path[a])==1)
    pcnt++;
    if(pcnt>1){
    for(int b=0;b<pnt;b++){
    printf("%d ",path[b]); //printing of the path
    }
    printf("\n");
    break;
    }
    }






    }

    else{
    if(j+1<y){ //for moving right


    path[pnt]=ar[i][j+1];
    int pnt1=pnt+1;
    move(x,y,ar,i,j +1,pnt1,path,i1 ,j1); //passing j+1 as j
    move(x,y,ar,i,j +1,i1,j1);
    }

    if(i+1<x){
    //for moving down


    path[pnt]=ar[i+1][j];
    int pnt1=pnt+1;
    move(x,y,ar,i+1 ,j,pnt1,path,i1 ,j1); //passing i+1 as i
    move(x,y,ar,i+1 ,j,i1,j1);
    }
    }



    }



    int main(){

    int n=7+rand()%6;
    int arp[n][n],a,b; //initial array

    for(a=0;a<n;a++ ){ //giving random values n printing the initial array
    for(b=0;b<n;b++ ){
    arp[a][b]=10+rand()%90;
    printf("%d ",arp[a][b]);
    }
    printf("\n");
    }

    int i=0+rand()%(n-3),path[1]; //choosing the start point
    int j=0+rand()%(n-3);

    int i1=i+2+rand()%( n-1-i-1); //choosing the end point
    int j1=j+2+rand()%( n-1-j-1);

    printf("\n\n%d( %d, %d) %d(%d, %d)\n\n",arp[i][j],i,j,arp[i1][j1],i1,j1); //printing the start and end point index

    int pnt=0;

    int x=i1-i+1, y=j1-j+1;

    int ar[i1-i+1][j1-j+1];

    for(a=0;a<=(i1-i);a++){ //creating another array with start as first and target as its last element
    for(b=0;b<=(j1-j);b++){
    ar[a][b]=arp[i+a][j+b];
    printf("%d ",ar[a][b]);
    }
    printf("\n");
    }

    move(x,y,ar,i,j ,pnt,path,i1,j1 ); //calling the function





    return 0;
    }



    //please help me in debugging my code
    Last edited by kamlesh1; Oct 28 '17, 05:43 PM. Reason: i am not albe to find the problem, please help
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You appear to be using C. I don't see any malloc() calls to create your array. That makes your array a local variable in some function. That makes the array location the stack frame of the function. Some systems place limits on the size of your stack frame. Exceed the limit and down you go.

    I recommend you use malloc() to create your array and then use free() when you are finished with it.

    Post again and tell me what happened.

    Comment

    Working...