By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,542 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

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

P: 1
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;
return 0;

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 start is same as target, it will check the prime in the path
int pcnt=0;
for(int a=0;a<pnt;a++){
for(int b=0;b<pnt;b++){
printf("%d ",path[b]); //printing of the path


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

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

//for moving down

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


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
printf("%d ",arp[a][b]);

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
printf("%d ",ar[a][b]);

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

return 0;

//please help me in debugging my code
Oct 28 '17 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
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.
Oct 29 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.