Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ pointer to new memory storing gibberish
    primarykey
    data
    text
    <p>I have 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.</p> <p>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.</p> <pre><code>#include &lt;iostream&gt; #include &lt;cstring&gt; using std::cout; using std::endl; class CStr { private: int m_length; char* m_pstr; public: // Constructor CStr (const char* s = "Default String") { cout &lt;&lt; "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&amp; aStr) { cout &lt;&lt; "Copy constructor called\n"; m_length = aStr.m_length; // Get length of the object to be copied m_pstr = new char [m_length]; // Allocate space for the string strcpy_s(m_pstr, m_length, aStr.m_pstr); // Copy string to memory } // Assignment operator CStr&amp; operator=(const CStr&amp; aStr) { cout &lt;&lt; "Assignment operator called\n"; if(this == &amp;aStr) // Check addresses, if equal return *this; // return the 1st operand m_length = aStr.m_length; // Get length of the rhs operand delete [] m_pstr; // Release memory for the lhs operand m_pstr = new char[m_length]; // Allocate space for the string strcpy_s(m_pstr, m_length, aStr.m_pstr); // Copy rhs operand string to the lhs operand return *this; // Return a reference to the lhs operand } // Addition operator CStr operator+(const CStr&amp; aStr) const { cout &lt;&lt; "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-&gt;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-&gt;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 &lt;&lt; Print() &lt;&lt; " 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload