RunRun-Time Check Failure #2 - Stack around the variable 'word' was corrupted.

I'm getting this error right before the dialog box closes:
Run-Time Check Failure #2 - Stack around the variable 'word' was corrupted.

here is my code:
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// test 22 j3 tackle 1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string> 
#include <math.h>

struct button
{
	string l;
	int x;
	int y;
} a, b, c, d, e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, space, minus, dot, enter;


int takeword(char in[40]);

int _tmain(int argc, _TCHAR* argv[])
{
	a.x = 0;
	a.y = 0;
	a.l = 'a';

	b.x = 1;
	b.y = 0;
	b.l = 'b';
	
	c.x = 2;
	c.y = 0;
	c.l = 'c';
	
	d.x = 3;
	d.y = 0;
	d.l = 'd';

	e.l = 'e';
	e.x = 4;
	e.y = 0;
	
	f.x = 5;
	f.y = 0;
	f.l = 'f';
	
	g.x = 0;
	g.y = 1;
	g.l = 'g';

	h.x = 1;
	h.y = 1;
	h.l = 'h';
		
	i.x = 2;
	i.y = 1;
	i.l = 'i';

	j.x = 3;
	j.y = 1;
	j.l = 'j';
	
	k.x = 4;
	k.y = 1;
	k.l = 'k';

	l.x = 5;
	l.y = 1;
	l.l = 'l';
	
	m.x = 0;
	m.y = 2;
	m.l = 'm';

	
	n.x = 1;
	n.y = 2;
	n.l = 'n';

	o.x = 2;
	o.y = 2;
	o.l = 'o';
	
	p.x = 3;
	p.y = 2;
	p.l = 'p';
	
	q.x = 4;
	q.y = 2;
	q.l = 'q';
	
	r.x = 5;
	r.y = 2;
	r.l = 'r';

	
	s.x = 0;
	s.y = 3;
	s.l = 's';

	t.x = 1;
	t.y = 3;
	t.l = 't';
	
	u.x = 2;
	u.y = 3;
	u.l = 'u';

	
	v.x = 3;
	v.y = 3;
	v.l = 'v';


	w.x = 4;
	w.y = 3;
	w.l = 'w';

	x.x = 5;
	x.y = 3;
	x.l = 'x';
	
	y.x = 0;
	y.y = 4;
	y.l = 'y';
	
	z.x = 1;
	z.y = 4;
	z.l = 'z';

	space.x = 2;
	space.y = 4;
	space.l = ' ';

	
	minus.x = 3;
	minus.y = 4;
	minus.l = '-';

	dot.x = 4;
	dot.y = 4;
	dot.l = '.';
	
	enter.x = 5;
	enter.y = 4;
	


	int total;
	cout << "Type in a word ";
	char word[40];
	//gets(word);
	//string word;
	cin >> word;


	total = takeword(word);
	cout << total << endl;

	system("pause");
	return 0;
}

int takeword(char in[40])
//int takeword(string in)
{
	int fix, lax, fiy, lay, tot;
	int num;

	for(num = 0;num < 40; num++)
	{
		if(in[num] = 'a')
		{
			fix = a.x;
			fiy = a.y;
		}

		if(in[num + 1] = 'b')
		{
			lax = b.x;
			lay = b.y;
		}

		tot =  abs(fix - lax) + abs(fiy - lay); 
	}
	tot  += abs(lax - 5) + abs(lay - 4);
		
	return (tot);
}


the problem is around line 150.
I tried switching from char to string but that made things worse.
If you are compiling with VC++ 2008 EE, I can tell you one thing: if you use debug configuration it will sometimes come out with these errors and notify you at the end. I believe, although I am not sure, it is because VC connects a debug module to your program after compilation to check for errors like those. Try changing the configuration to release. (The config manager is not too hard to find but I don't actually remember where it is, sorry)
If you are not using VC++ then I cannot say, I am afraid I have no answers for you.
By the way you would be better off using a string. How does it make things worse for you anyway?
Try changing the configuration to release.


This does not solve the problem, it just hides the problem.

It's like lighting a room on fire and then "fixing" it by closing the door so you can't see the fire anymore.


(why oh why are people so afraid of std::string)

"Stack corruption" means that you're writing something on the stack that you shouldn't be. 9999 times in 10000 (or more) this is cause by either

a) a bad pointer
or
b) a buffer overflow

Since you're not dereferencing any pointers here (that I can see), my money is on a buffer overflow.

Here's my bet:

1
2
3
4
5
  char word[40];

  cin >> word;  // what if the user inputs more than 39 characters?
     // Answer:  BUFFER OVERFLOW
     //  since the buffer is on the stack, this = stack corruption 


You're already using string in your program -- so why aren't you using it here?

Just change 'word' to be a string instead of a char array:

1
2
  string word;
  cin >> word;  // problem solved 


takeword() might have the same problem. It seems you just assume exactly 40 characters are input instead of checking to see how many characters were actually input. Don't do that.
Thanks it worked.
I also found that changing cin >> word to gets(word) also fixes the problem.

As for changing my int takeword(char in[20]) funtion to int takeword(string in) I get this message:

" Microsoft Visual Studio C Runtime Library has detected a fatal error in test 22 j3 tackle 1.exe. "
Lines 171 and 177: you are using assignment ( = ) instead of comparison ( == )
Line 177: num+1 is over the array bounds when num==39
With std::strings you won't have these problems
Topic archived. No new replies allowed.