C++ pointer to new memory storing gibberish
I a class that accepts character arrays. I realize that std::string is
preferred for working with strings but this is an excercise to learn about
pointers and allocating dynamic memory. The problem I am having is with
the overloaded addition operator. I allocate new memory to the size of the
two char arrays being concatenated, plus a terminating null character.
When I put breakpoints on the for loops I can see the program is iterating
through the left hand operand pointer(p) and then the right hand operand
pointer(q), but the individual chars are not being stored to temp_str.
I am fairly new to C++ so I am sure I am missing something basic about
pointers and/or dynamic memory. Any help/advice would be appreciated.
Thanks.
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
class CStr
{
private:
int m_length;
char* m_pstr;
public:
// Constructor
CStr (const char* s = "Default String")
{
cout << "Constructor called\n";
m_length = strlen(s) + 1; // Store the length of the string
m_pstr = new char[m_length]; // Allocate space for the string
strcpy_s(m_pstr,m_length,s); // Copy string to memory
}
// Copy constructor
CStr(const CStr& aStr)
{
cout << "Copy constructor called\n";
size_t tempLength = strlen(aStr.m_pstr)+1; // Get length of
the object to be copied
m_pstr = new char [tempLength]; // Allocate space
for the string
strcpy_s(m_pstr, tempLength, aStr.m_pstr); // Copy string to
memory
}
// Assignment operator
CStr& operator=(const CStr& aStr)
{
cout << "Assignment operator called\n";
if(this == &aStr) // Check
addresses, if equal
return *this; // return the
1st operand
size_t tempLength = strlen(aStr.m_pstr)+1; // Get length
of the 2nd operand
delete [] m_pstr; // Release
memory for the 1st operand
m_pstr = new char[tempLength]; // Allocate
space for the string
strcpy_s(this->m_pstr, tempLength, aStr.m_pstr); // Copy 2nd
operand string to the 1st operand
return *this; // Return a
reference to the 1st operand
}
// Addition operator
CStr operator+(const CStr& aStr) const
{
cout << "Addition operator called\n";
// get the lengths of the strings
size_t rhs_length = strlen(aStr.m_pstr); //
length of rhs operand
size_t lhs_length = strlen(this->m_pstr); //
length of lhs operand
char* temp_str = new char[lhs_length + rhs_length + 1]; //
Allocate memory to hold concatenated string
//
plus
terminating
null
character
char* p = this->m_pstr;
char* q = aStr.m_pstr;
for (p; *p!=0; p++) // Increment lhs string pointer
*temp_str++ = *p; // Store character
for (q; *q!=0; q++) // Increment rhs string pointer
*temp_str++ = *q; // Store character
*temp_str++ = '\0'; // Null character at the end
return CStr(temp_str);
}
// Destructor
~CStr ()
{
cout << Print() << " has been destroyed\n";
delete [] m_pstr; // Free memory
assigned to the pointer
}
// Print function
char* Print() const
{
return m_pstr;
}
};
int main()
{
CStr s1("foo");
CStr s2("bar");
CStr s3 = s1 + s2;
}
No comments:
Post a Comment