title: Segmentation Fault with Dynamic Class (homework problem)
body: <p>I'm attempting to convert a working static String class to a dynamic String class using pointers, but when I implement the pointer it throws a segmentation fault error. Code below:</p>
  
  <p>mystring1.h:</p>
  
  <pre><code>//File: mystring1.h
  // Declaration file for user-defined String class.
  
  
  #ifndef _MYSTRING_H
  #define _MYSTRING_H
  #include&lt;iostream&gt;
  #include &lt;cstring&gt;
  using namespace std;
  
  #define MAX_STR_LENGTH  200
  
  
  class String {
  public:
    String();
    String(const char *s);  // a conversion constructor   
    void append(const String &amp;str);
  
    //deconstructor
    ~String();
  
    //copy constructor
    String (const String &amp; origString);
  
    //assignment operator overload
    String operator =(const String &amp;origString);
  
    // Relational operators
    bool operator &gt;(const String &amp;str) const;
    bool operator &lt;(const String &amp;str) const;    
  
    String operator +=(const String &amp;str);      
  
    void print(ostream &amp;out) const;    
    int length() const;
  
    char operator [](int i) const;  // subscript operator 
  
  
  
  private:
      char *contents;
      int len;
      //int capacity;
  };
  
  ostream &amp; operator&lt;&lt;(ostream &amp;out, const String &amp; r); // overload ostream operator "&lt;&lt;" - External!  
  
  
  #endif /* not defined _MYSTRING_H */
  </code></pre>
  
  <p>mystring1.cpp:</p>
  
  <pre><code>//File: mystring1.h
  // Implementation file for user-defined String class.
  
  #include "mystring1.h"
  
  
  String::String()
  {
    contents[0] = '\0';
    len = 0;
  }
  
  String::String(const char s[])
  {
    len = strlen(s);
    contents = new char[len + 1];
    strcpy(contents, s);
  }
  
  
  
  void String::append(const String &amp;str)
  {
    strcat(contents, str.contents);
    len += str.len;
  }
  
  //deconstructor
  String::~String()
  {
    delete [] contents;
  }
  
  //copy constructor
  String::String(const String &amp;origString)
  {
    len = origString.len;
    contents = new char[len + 1];
  
    for (int i = 0; i &lt; len; i++)
    {
      contents[i] = origString.contents[i];
    }
  }
  
  //assignment operator overload
  String String::operator =(const String &amp;origString)
  {
    if (this != &amp;origString)
    {
      len = origString.len;
      delete [] contents;
      contents = new char[len + 1];
  
      for (int i = 0; i &lt; len; i++)
      {
        contents[i] = origString.contents[i];
      }
    }
  
    return *this;
  }
  
  
  bool String::operator &gt;(const String &amp;str) const
  {
    return strcmp(contents, str.contents) &gt; 0;
  }
  
  bool String::operator &lt;(const String &amp;str) const
  {
    return strcmp(contents, str.contents) &lt; 0;
  }
  
  
  String String::operator +=(const String &amp;str)
  {
      append(str);
      return *this;
  }
  
  void String::print(ostream &amp;out) const
  {
    out &lt;&lt; contents;
  }
  
  int String::length() const
  {
    return len;
  }
  
  char String::operator [](int i) const
  {
    if (i &lt; 0 || i &gt;= len) {
      cerr &lt;&lt; "can't access location " &lt;&lt; i
           &lt;&lt; " of string \"" &lt;&lt; contents &lt;&lt; "\"" &lt;&lt; endl;
      return '\0';
    }
    return contents[i];
  }
  
  ostream &amp; operator&lt;&lt;(ostream &amp;out, const String &amp; s) // overload ostream operator "&lt;&lt;" - External!
  { 
      s.print(out);
      return out;
  }
  
  </code></pre>
  
  <p>Assignment 5 Driver:</p>
  
  <pre><code>/**
   * cmpsc122 Assignment 5 test file
   * File Name: Assign5driver.cpp
   *
   * Description: This program demonstrates a basic String class that implements
   * dynamic allocation and operator overloading.
   *
   */
  
  #include &lt;iostream&gt;
  #include "mystring1.h"
  using namespace std;
  
  
  /*************************** Main Program **************************/
  
  int main()
  {
    String str1, str2("dog");  // Using constructor for initial strings
    char s1[100], s2[100];              // Some C strings.
  
    // Print out initial strings
  
    cout &lt;&lt; "Initial values:" &lt;&lt; endl;
    cout &lt;&lt; "str1 holds \"" &lt;&lt; str1 &lt;&lt; "\" (length = " &lt;&lt; str1.length() &lt;&lt; ")" &lt;&lt; endl;
    cout &lt;&lt; "str2 holds \"" &lt;&lt; str2 &lt;&lt; "\" (length = " &lt;&lt; str2.length() &lt;&lt; ")" &lt;&lt; endl;
  
    // Inputs some new strings in them
  
    cout &lt;&lt; "\nEnter a value for str1 (no spaces): ";
    cin &gt;&gt; s1;
    str1 = s1;
  
    cout &lt;&lt; "\nEnter a value for str2 (no spaces): ";
    cin &gt;&gt; s2;
    str2 = s2;
  
  
    cout &lt;&lt; "\nAfter assignments..." &lt;&lt; endl;
    cout &lt;&lt; "str1 holds \"" &lt;&lt; str1 &lt;&lt; "\" (length = " &lt;&lt; str1.length() &lt;&lt; ")" &lt;&lt; endl;
    cout &lt;&lt; "str2 holds \"" &lt;&lt; str2 &lt;&lt; "\" (length = " &lt;&lt; str2.length() &lt;&lt; ")" &lt;&lt; endl;
  
    // Get some elements...
  
    int i;
  
    cout &lt;&lt; "\nEnter which element of str1 to display: ";
    cin &gt;&gt; i;
    cout &lt;&lt; "Element #" &lt;&lt; i &lt;&lt; " of str1 is '" &lt;&lt; str1[i] &lt;&lt; "'" &lt;&lt; endl;
  
    cout &lt;&lt; "\nEnter which element of str2 to display: ";
    cin &gt;&gt; i;
    cout &lt;&lt; "Element #" &lt;&lt; i &lt;&lt; " of str2 is '" &lt;&lt; str2[i] &lt;&lt; "'" &lt;&lt; endl;
  
    // Concate some strings
  
    cout &lt;&lt; "\nEnter a value to append to str1 (no spaces): ";
    cin &gt;&gt; s1;
   // str1.append(s1); // Actually, the cstring is converted to String object here by the constructor
    str1 += s1;   // same as above
  
    cout &lt;&lt; "\nEnter a value to append to str2 (no spaces): ";
    cin &gt;&gt; s2;
    str2 += s2;
  
  
  
    cout &lt;&lt; "\nAfter appending..." &lt;&lt; endl;
    cout &lt;&lt; "str1 holds \"" &lt;&lt; str1 &lt;&lt; "\" (length = " &lt;&lt; str1.length() &lt;&lt; ")" &lt;&lt; endl;
    cout &lt;&lt; "str2 holds \"" &lt;&lt; str2 &lt;&lt; "\" (length = " &lt;&lt; str2.length() &lt;&lt; ")" &lt;&lt; endl;
  
    // Compare strings...
  
    cout &lt;&lt; "\nComparing str1 and str2..." &lt;&lt; endl;
  
    cout &lt;&lt; "\"";
  
    cout&lt;&lt; str1;   // test the overloading of ostream operator &lt;&lt;
  
    cout &lt;&lt; "\" is ";
  
    if (str1 &lt; str2) {      // test the overloading of comparison operator &lt;
      cout &lt;&lt; "less than";
    } else if (str1 &gt; str2) {
      cout &lt;&lt; "greater than";
    } else {
      cout &lt;&lt; "equal to";
    }
  
    cout &lt;&lt; " \"";
    cout &lt;&lt; str2;
    cout &lt;&lt; "\"" &lt;&lt; endl;
  
    cout &lt;&lt; "\ntest the = operator, after str1 = str2; "&lt;&lt; endl;
  
    str1 = str2;
  
   cout &lt;&lt; "str1 holds \"" &lt;&lt; str1 &lt;&lt; "\" (length = " &lt;&lt; str1.length() &lt;&lt; ")" &lt;&lt; endl;
    cout &lt;&lt; "str2 holds \"" &lt;&lt; str2 &lt;&lt; "\" (length = " &lt;&lt; str2.length() &lt;&lt; ")" &lt;&lt; endl;
  
    str1 += s1;
  
    cout &lt;&lt; "\nAfter str1 = str1 + s1: "&lt;&lt; endl;
  
    cout &lt;&lt; "str1 holds \"" &lt;&lt; str1 &lt;&lt; "\" (length = " &lt;&lt; str1.length() &lt;&lt; ")" &lt;&lt; endl;
    cout &lt;&lt; "str2 holds \"" &lt;&lt; str2 &lt;&lt; "\" (length = " &lt;&lt; str2.length() &lt;&lt; ")" &lt;&lt; endl;
  
    String str3(str2);
    cout &lt;&lt; "\ntest the copy constructor, after str4(str3);"&lt;&lt; endl;
  
    cout &lt;&lt; "str2 holds \"" &lt;&lt; str2 &lt;&lt; "\" (length = " &lt;&lt; str2.length() &lt;&lt; ")" &lt;&lt; endl;
    cout &lt;&lt; "str3 holds \"" &lt;&lt; str3 &lt;&lt; "\" (length = " &lt;&lt; str3.length() &lt;&lt; ")" &lt;&lt; endl;
  
    cout &lt;&lt; "\nafter appending str2 by str1" &lt;&lt; endl;
    str2 += str1;
    cout &lt;&lt; "str2 holds \"" &lt;&lt; str2 &lt;&lt; "\" (length = " &lt;&lt; str2.length() &lt;&lt; ")" &lt;&lt; endl;
    cout &lt;&lt; "str3 holds \"" &lt;&lt; str3 &lt;&lt; "\" (length = " &lt;&lt; str3.length() &lt;&lt; ")" &lt;&lt; endl;
  
    cout&lt;&lt; "\nstr3 are not changed. Type any letter to quit." &lt;&lt; endl;
  
    char q;
  
    cin &gt;&gt; q;
  
    return 0;
  }
  </code></pre>
  
  <p>Any help would be appreciated, I have tried all that I can and my research into segmentation fault causes isn't doing much to help.</p>