Corrupted stack

Hi.
The next code will do this:
In the matrix
a b c d
e f g h
i j k l
m n o p
it finds all 3 character words that can be done by starting first from the char 'a' and then you can choose a letter from a neighbour and etc. then starting from char 'b' and etc.

It works with 4 character words if you change the if(n!=2) with if(n!=3) where I check if I am at the last character. But when I try to make it with 5 character words it says "Stack around var duma is corrupted"

Can you please tell me why?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
#include<string.h>
using namespace std;



int tyrsene3(char matrica[4][4],int n,int a,int b,int a1[4][4],char duma[10])
{ 
  if(a<0||a>3||b<0||b>3)
  {return 0;}
  else
  {
	
	if(a1[a][b]==0)
     {      
        if(n!=2)//Here I check if I am at the last character
             { duma[n]=matrica[a][b];
              a1[a][b]=1;
		        n++;
	              tyrsene3(matrica,n,a-1,b-1,a1,duma);
	              tyrsene3(matrica,n,a-1,b,a1,duma);
	              tyrsene3(matrica,n,a-1,b+1,a1,duma);
	              tyrsene3(matrica,n,a,b-1,a1,duma);
	              tyrsene3(matrica,n,a,b+1,a1,duma);
	              tyrsene3(matrica,n,a+1,b-1,a1,duma);
                       tyrsene3(matrica,n,a+1,b,a1,duma);
	              tyrsene3(matrica,n,a+1,b+1,a1,duma);
               a1[a][b]=0; 
			   n--;
			   return 0;
		       }
            else
             {   
	     
				 duma[n]=matrica[a][b];
				 duma[n+1]='\0';
         
				  cout<<duma<<" ";
				return 1;
		      }

	   }
	else return 0;
  }
 
}





void dumichka(char matrica[][4],int start, int end,int a1[4][4])
{ char duma[5];
 for(int i=0;i<4;i++)
	 for(int j=0;j<4;j++)
			a1[i][j]=0;
  
tyrsene3(matrica,0,start,end,a1,duma);

}
int main()
{
	 char matrica[4][4];
	 int a1[4][4];
	 char bukva='a';
	 for(int i=0;i<4;i++)
		 for(int j=0;j<4;j++)
			 {matrica[i][j]=bukva;
	 bukva++;}
    for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
	 dumichka(matrica,i,j,a1);
	
	 return 0;
}
Last edited on
It works with 4 character words if you change the if(n!=2) with if(n!=3) where I check if I am at the last character. But when I try to make it with 5 character words it says "Stack around var duma is corrupted"

Can you please tell me why?

Because you are probably trying to access your arrays out of bounds. Did you remember to change the size of your arrays to accommodate this larger size? This is one of the reasons why using "Magic Numbers" should be avoided.
Last edited on
I'm sorry I didn't exactly understand what did you mean? (it's probably because of my bad English) Can you give me an example? And what is that "Magic Numbers"
And what is that "Magic Numbers"

http://en.wikipedia.org/wiki/Magic_number_%28programming%29
Be sure to read the section titled: "Unnamed numerical constants".

I'm sorry I didn't exactly understand what did you mean?
What is the size of your arrays?

What happens when you try to access an array using an index that is larger than the array size (out of bounds)?

Last edited on
Well I don't think the size of the 'matrica' array should be changed, because I dont want to change the matrix but the lenth of the made word. Also the array 'duma' is sized 10 so it should be big enough to take, a five char word and the '/0' sign.
As I read the article you gave me I think you mean that I used magical numbers in this if(n!=2) is that right?
Look at the following snippet:
1
2
3
4
5
	 char matrica[4][4];
	 int a1[4][4];
	 char bukva='a';
	 for(int i=0;i<4;i++)
		 for(int j=0;j<4;j++)

In my opinion all those fours are magic numbers. If you created a constant for that number then used it in place of the 4 everywhere in your program, your program would be better.

Oh, Yeah that's right I am 100% agreed with you. But I don't think that is linked with my problem
Also the array 'duma' is sized 10 so it should be big enough to take, a five char word and the '/0' sign.

No, it's not.

int tyrsene3(char matrica[4][4],int n,int a,int b,int a1[4][4],char duma[10])

is exactly the same as:

int tyrsene3(char matrica[][4],int n,int a,int b,int a1[][4],char duma[])

Which is to say that the sizes specified for arrays here have no effect at all.


The array you are feeding to tyrsene3 is defined here:
1
2
3
4
5
6
7
8
9
void dumichka(char matrica[][4],int start, int end,int a1[4][4])
{ 
    char duma[5];
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            a1[i][j]=0;
  
    tyrsene3(matrica,0,start,end,a1,duma);
}


and as you can see, it isn't large enough to take "a five char word and the '/0' sign."
Last edited on
aw.. yeah.. thank you very much!! I dont know why I haven't seen that.. but I am new...I guess is ok to make these mistakes. Really thank you very much!
Yet another reason not to use those "magic numbers". If you used a constant you would just need to change that one constant instead of all the locations you now need to change.
Topic archived. No new replies allowed.