I have the following files for implementing a Bucket
class. However I can't destroy the _str
member in the destructor of the Bucket
s produced by the operator+
. The error that I get is:
heap corruption detected after normal block...CRT detected that the application wrote to memory after end of heap buffer
The problem always occurs after the addition of the two Bucket
objects, however I've checked during the debugging that the produced string is correct as well as the length.
Also, how can I access the _len
attribute in the operator>>
function in order to assign a new value?
Header File:
class Bucket
{
friend ostream& operator<<(ostream& os, const Bucket& c);
friend istream& operator>>(istream& is, const Bucket& c);
public:
Bucket();
Bucket(const String& str);
Bucket(const char* str);
~Bucket();
const Bucket operator+(const Bucket& s) const;
const Bucket operator+(const char* s) const;
const Bucket& operator=(const char* c);
const bool operator==(const char* str);
private:
char* _str;
int _len;
};
Source File:
Bucket::Bucket() {
_len = 0;
_str = new char[_len];
}
Bucket::Bucket(const Bucket& myBucket) {
_len = myBucket._len;
_str = new char[_len + 1];
for (int i = 0; i < _len; i++)
_str[i] = myBucket._str[i];
_str[_len] = '\0';
}
Bucket::Bucket(const char* str) {
_len = 0;
for (int i = 0; str[i] != '\0'; i++) _len++;
_str = new char[_len + 1];
for (int i = 0; i < _len; i++)
_str[i] = str[i];
_str[_len] = '\0';
}
Bucket::~Bucket() {
if (_len > 0) delete[] _str;
}
const Bucket Bucket::operator+(const Bucket& myBucket) const {
String tempBucket;
tempBucket._str = strcat(this->_str, myBucket._str);
int len = 0;
while (tempBucket._str[len] != '\0') len++;
tempBucket._str[len] = '\0';
tempBucket._len = len;
return tempBucket;
}
const Bucket Bucket::operator+(const char* str) const {
Bucket tempBucket;
int len = 0;
tempBucket._len = this->_len;
for (int i = 0; str[i] != '\0'; i++) tempBucket._len++;
tempBucket._str = strcat(tempBucket._str, str);
tempBucket._str[len] = '\0';
tempBucket._len = len;
return tempBucket;
}
const Bucket& Bucket::operator=(const char* str) {
if (this->_str == str) {
return *this;
}
_len = 0;
for (int i = 0; str[i] != '\0'; i++) _len++;
_str = new char[_len + 1];
for (int i = 0; i < _len; i++)
_str[i] = str[i];
_str[_len] = '\0';
return *this;
}
const bool Bucket::operator==(const char* str) {
int comp = strcmp(this->_str, str);
if (comp == 0) {
return true;
}
else {
return false;
}
}
ostream& operator<<(ostream& os, const Bucket& myBucket) {
os << myBucket._str;
return os;
}
istream& operator>>(istream& is, const Bucket& myBucket) {
static char buffer[40];
is >> buffer;
int len = 0;
for (size_t i = 0; buffer[i] != '\0'; i++) {
myBucket._str[i] = buffer[i];
len++;
}
myBucket._str[len++] = '\0';
return is;
}
Main:
int main()
{
Bucket b1("Hello, "); // This is deleted by the destructor
Bucket b2(b1); // This is deleted by the destructor
cout << b1 << b2 << endl;
b2 = "Dear "; // Also works fine
Bucket b3;
cout << "Enter a name: ";
cin >> b3; // Can't assign the _len attribute
cout << b2 + b3 << ","; // not destroyed
Bucket b4(" Please write this sentence after pressing enter:\n");
b2 = "We believe that ";
cout << b4 + b2 << b1 << "and " << "Goodbye " // not destroyed
<< (b1 == "Goodbye " ? "is " : "is not ") << "the same word!\n" <<
endl;
return 0;
}