COM SCI 32 Lecture Notes - Lecture 11: Empty String, Null Pointer, Memory Management

54 views5 pages

Document Summary

Copy constructor and assigment operator class string public: // m_text points to a dynamically allocated array of m_len+1 chars. // m_text[m_len] == "/0" char* m_text; int m_len; String::string(const char* value) m_len = strlen(value); m_text = new char[m_len + 1]; strcpy(m_text, value); String::~string() delete [] m_text; void f(string t) void g() } //when we leave g(), s goes away, but hello is still there char* somefunc(); int main() The (cid:210)rst thing that goes wrong is strlen() . If we enter the constructor, and the parameter value is nullptr , it sets the value instead to an empty string. We can make value itself point to another pointer. const char* value simply means that value points to a constant, so the pointer stored in value can"t be changed. We can change value , to be clear. String::string(const char* value) if(value == nullptr) value = ; m_len = strlen(value); m_text = new char[m_len + 1]; strcpy(m_text, value);

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents

Related Questions