Help with Error

So I am working on an assignment and it works for the most part but at the end I get an error message that says Heap corruption detected after normal block (#137) CRT detected that the application wrote to memory after end of heap. I have been working on this for a while and can't figure out what I am doing wrong. Here is the 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
#include "Text.h"
 
 
Text::Text ( const char *charSeq)
{
  bufferSize = (unsigned)strlen(charSeq);
 
	buffer = new char [bufferSize];
	for(int i = 0; i <= bufferSize; i++){ 
		buffer[i] = charSeq[i];
	}
}
 
Text::Text ( const Text &other )
{
	bufferSize = other.getLength();
	buffer = new char[bufferSize];
	for(int i = 0; i <= bufferSize; i++){
		buffer[i] = other[i];
	}
}
 
void Text::operator = ( const Text &other )
{
	if (this != &other){
		delete [] buffer;
		bufferSize = other.getLength(); 
		buffer = new char[bufferSize];
		for (int i = 0; i < bufferSize; i++){
			buffer[i] = other[i];
		}
	}
}
 
Text::~Text ()
{
	//...
	delete [] buffer;
}
 
int Text::getLength () const
{
	return bufferSize;
}
 
char Text::operator [] ( int n ) const
{
	if (n >= 0 && n < bufferSize){
		return buffer[n];
	}
	else{
		return '\0';
	}
}
 
void Text::clear ()
{
	delete [] buffer;
	buffer = new char[bufferSize];
	for(int i=0; i < bufferSize;i++){
		buffer[i] = ' ';
	}
}
 
void Text::showStructure () const
{
	for (int i=0;i < bufferSize;i++){
		cout << buffer[i];
	}
	cout << endl;
}
 
Text Text::toUpper( ) const
{
	Text temp(buffer);
	for(int i=0; i < bufferSize; i++){
		if((buffer[i] > 96) && (buffer[i] < 123)){
			temp.buffer[i] = char(buffer[i] - 32);
		}
	}
	return temp;
}
 
Text Text::toLower( ) const
{
	Text temp(buffer);
	for(int i=0; i < temp.getLength(); i++){
		if((buffer[i] > 64) && (buffer[i] < 91)){
			temp.buffer[i] = char(buffer[i] + 32);
		} 
	}
	return temp;
}
 
bool Text::operator == ( const Text& other ) const
{
	if(bufferSize == other.getLength()){
		if(bufferSize == 0){
			return true;
		}
		else{
			for(int i = 0; i < bufferSize; i++){
				if(buffer[i] != other.buffer[i]){
					return false;
				}
			}
			return true;
		}
	} 
	else{
		return false;
	}
}
 
bool Text::operator <  ( const Text& other ) const
{
	if (other.getLength()){
		if(bufferSize){
		    for(int i = 0; i < bufferSize; i++){
			    if (buffer[i] > other.buffer[i]){
				    return false;
			    }
			}
		    if((other.getLength() < bufferSize) || (other.getLength() == bufferSize)){
			    return false;
			}
		    else{
			    return true;
		    }
		}
		else{
			return true;
		}
	}
	return false;
}
 
bool Text::operator >  ( const Text& other ) const
{
	if(bufferSize){
		if(other.getLength()){
		    for(int i = 0; i < other.getLength(); i++){
			    if (buffer[i] < other.buffer[i]){
				    return false;
			    }
			}
		    if((other.getLength() > bufferSize) || (other.getLength() == bufferSize)){
			    return false;
			}
		    else{
			    return true;
		    }
		}
		else{
			return true;
		}
	}
	return false;
}
Look at the following snippet:
1
2
3
4
5
  bufferSize = (unsigned)strlen(charSeq);
 
	buffer = new char [bufferSize];
	for(int i = 0; i <= bufferSize; i++){ 
		buffer[i] = charSeq[i];


First why the C style cast on the first line. The strlen() function returns a size_t, which is how bufferSize should be defined. A size_t is usually an unsigned int, but could be an unsigned long or some other unsigned type, which is implementation defined. So you should be using size_t not unsigned.

Next you appear to have a possible buffer overrun in that loop. Remember arrays start at zero and stop at size - 1. Because you use the <= operator you're stopping at size. Also without seeing the rest of your code it might be possible that charSeq is not terminated, and if that's the case then your strlen() call could be giving you problems as well.

Next look at this snippet:
1
2
3
4
5
6
7
8
void Text::clear ()
{
	delete [] buffer;
	buffer = new char[bufferSize];
	for(int i=0; i < bufferSize;i++){
		buffer[i] = ' ';
	}
}


Why are you deleting the buffer? Since you fill the buffer with a character you probably don't need this operation. Also be careful, since you fill the buffer with the 'space' you can't use the C-string functions like strlen() on this array of char, since it is not a C-string because it is not terminated properly.


Lines 8 and 17: You allocate bufferSize characters.
Lines 9 and 18: Your termination conditions are faulty.

Lets assume bufferSize is 10. Your allocated array elements are [0]-[9], but the <= condition causes you to reference buffer[10], which is past the end of the array.
Topic archived. No new replies allowed.