How to fix error
Sep 23, 2014 at 7:30pm UTC
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 ;
}
Sep 23, 2014 at 7:47pm UTC
If you want to write the null character to the buffer you need to allocate one extra character (bufferSize + 1).
Sep 23, 2014 at 8:01pm UTC
I'm getting confused because someone told me something similar but it was not where the Null was written at. What line are you referring to?
Thank you
Sep 23, 2014 at 8:05pm UTC
Sep 23, 2014 at 8:14pm UTC
Sorry. I'm extremely tired and thought I didn't post it before.
Sep 23, 2014 at 8:30pm UTC
Ok. I got it now. Thank you so much for the help I understand what I did wrong
Topic archived. No new replies allowed.