title: copy a string function
body: <p>I've tried to implement my own copy string function in c++ </p>
  
  <pre><code>void pcstrdup(const char* szStr, char* szStrCpy)
  {
      int nLen = strlen(szStr);
      if (!nLen)
          throw "Error : attempt copying an empty string";
      ++nLen;
      szStrCpy = static_cast&lt;char*&gt;(malloc(sizeof(char) * nLen));
      if (!szStrCpy)
          throw "Error : memory allocation failed";
      for (int i = 0; i &lt; nLen; i++)
      {
          szStrCpy[i] = szStr[i];
      }
  }
  </code></pre>
  
  <p>I've debugged and checked that the characters are being copied and it copies them all but the \0 character, when it gets to that point I get an exception</p>
  
  <pre><code>Unhandled exception at 0x011A5BA1 in assignment2.exe: 0xC0000005: Access violation reading location 0x00000000.
  </code></pre>
  
  <p>Which brings me to this function :</p>
  
  <pre><code>static size_t __CLRCALL_OR_CDECL length(const _Elem *_First)
      {   // find length of null-terminated string
      return (*_First == 0 ? 0
          : _CSTD strlen(_First));
      }
  </code></pre>
  
  <p>note that the istrlen() function, is a function I wrote.</p>
  
  <pre><code>int istrlen(const char* szStr)
  {
      int count = 0;
      for (int i = 0; szStr[i] != NULL; i++)
      {
          ++count;
      }
      return count;
  }
  </code></pre>