title: Using valgrind - "Invalid read of size 1" for strlen
body: <p>I'm trying to write code that sets the name of a Student object to a new name, but I'm coming across memory leak errors when creating a character array. I assume it has to do with /0 at the end of the array and isn't terminating properly, but I don't know how to properly fix this. Thanks for the help. </p>
  
  <pre><code>#include "student.h"
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  Student::Student(const char * const name, int perm) {
    this-&gt;setName(name);
    this-&gt;setPerm(perm);
  }
  
  int Student::getPerm() const {
    return this-&gt;perm;
  }
  
  const char * const Student::getName() const {
    return this-&gt;name;
  }
  
  void Student::setPerm(const int perm) {
    this-&gt;perm = perm;
  }
  
  void Student::setName(const char * const newName) {
    this-&gt;name = new char[strlen(newName)+1];
    // this-&gt;name[srtlen(newName)+1] = '/0';  &lt;---- My suggested fix, but doesn't work
    strcpy(this-&gt;name,newName);
  
  }
  
  
  Student::Student(const Student &amp;orig) {
    this-&gt;setName(orig.getName());
    this-&gt;setPerm(orig.getPerm());
  }
  
  Student::~Student() {
    delete this-&gt;name;
    this-&gt;perm = 0;
  }
  </code></pre>
  
  <p>This is the valgrind error:</p>
  
  <pre><code>==13814== Invalid read of size 1
  ==13814==    at 0x4C2BA12: strlen (vg_replace_strmem.c:454)
  ==13814==    by 0x4F56FD6: UnknownInlinedFun (char_traits.h:267)
  ==13814==    by 0x4F56FD6: std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;) (basic_string.h:456)
  ==13814==    by 0x401ED8: Student::toString[abi:cxx11]() const (student.cpp:64)
  ==13814==    by 0x401A46: main (testStudent00.cpp:14)
  ==13814==  Address 0x5302e8 is not stack'd, malloc'd or (recently) free'd
  ==13814== 
  </code></pre>