title: Accessing string array from class member function
body: <p>I'm using a basic class and trying to access a member string array in a member function.  I'm confused because the string array initializes and gets freed without any memory errors (Dr. Memory) if I only call the <code>set_words</code>, <code>print_words</code>, and <code>del_words</code> functions:</p>
  
  <pre><code>class XWORD {
    public:
      int vdir;
      int len;
      int wid;
      int nWords;
      char ** arr;
      char ** words;     
      ...
  };
  
  void XWORD::set_words(int istart, int inWords, char* iwords[]) {
      int w = istart;
      int k = 0;
      this-&gt;nWords = inWords;
  
      this-&gt;words = new char* [this-&gt;nWords];
      for (w=istart; w&lt;(istart+this-&gt;nWords); w++) {
          this-&gt;words[k] = new char [1+strlen(iwords[w])];
          this-&gt;words[k] = strcpy(this-&gt;words[k], iwords[w]);
          k++;
      }
  }
  
  void XWORD::del_words() {
      int w = 0;
      for (w=0; w&lt;(this-&gt;nWords); w++) {
          delete[] this-&gt;words[w];
      }
      delete[] this-&gt;words;
  }
  
  void XWORD::print_words() {
      int w = 0;
      for (w=0; w&lt;(this-&gt;nWords); w++) {
          printf("\n%s",this-&gt;words[w]);
      }
      printf("\n");
  }
  </code></pre>
  
  <p>However, when I call the following function, I get the Dr. memory errors shown below (<code>init_arr</code> and <code>del_arr</code> alone don't give errors either):</p>
  
  <pre><code>void XWORD::add_word_to_arr(int iw, int iy, int ix) {
      int k = 0;
      int y = iy;
      int x = ix;
  
      for (k=0; k&lt;(int)strlen(this-&gt;words[iw]); k++) {
          this-&gt;arr[y][x] = this-&gt;words[iw][k];
          if (this-&gt;vdir) {
              y++;
          } else {
              x++;
          }
      }
  }
  
  int main(int argc, char * argv[]) {
      XWORD x;
      x.set_words(1, argc-1, argv);
      x.init_arr(10,10);
  
      x.add_word_to_arr(0, x.len/2, x.wid/2); // WITHOUT THIS, NO ERRORS
      x.print_words();
  
      x.del_words();
      x.del_arr();
  }
  </code></pre>
  
  <p>Dr. Memory Errors:</p>
  
  <pre><code>c:\MinGW\WORKSPACE\cpp\xword&gt;make runmem
  drmemory -brief -batch bin/test.exe
  ~~Dr.M~~ Dr. Memory version 1.8.0
  ~~Dr.M~~ Running "bin/test.exe"
  ~~Dr.M~~
  ~~Dr.M~~ Error #1: UNADDRESSABLE ACCESS beyond heap bounds: reading 4 byte(s)
  ~~Dr.M~~ # 0 XWORD::add_word_to_arr               [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:301]
  ~~Dr.M~~ # 1 main                                 [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:302]
  ~~Dr.M~~ Note: refers to 0 byte(s) beyond last valid byte in prior malloc
  ~~Dr.M~~
  ~~Dr.M~~ Error #2: UNADDRESSABLE ACCESS: reading 1 byte(s)
  ~~Dr.M~~ # 0 replace_strlen                       [d:\drmemory_package\drmemory\replace.c:375]
  ~~Dr.M~~ # 1 XWORD::add_word_to_arr               [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:301]
  ~~Dr.M~~ # 2 main                                 [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:302]
  ~~Dr.M~~
  ~~Dr.M~~ Error #3: LEAK 0 bytes
  ~~Dr.M~~ # 0 replace_operator_new_array               [d:\drmemory_package\common\alloc_replace.c:2638]
  ~~Dr.M~~ # 1 XWORD::set_words                         [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:301]
  ~~Dr.M~~ # 2 main                                     [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:302]
  ~~Dr.M~~
  ~~Dr.M~~ Error #4: LEAK 40 direct bytes + 110 indirect bytes
  ~~Dr.M~~ # 0 replace_operator_new_array               [d:\drmemory_package\common\alloc_replace.c:2638]
  ~~Dr.M~~ # 1 XWORD::init_arr                          [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:301]
  ~~Dr.M~~ # 2 main                                     [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:302]
  ~~Dr.M~~
  ~~Dr.M~~ ERRORS FOUND:
  ~~Dr.M~~       2 unique,     2 total unaddressable access(es)
  ~~Dr.M~~       0 unique,     0 total uninitialized access(es)
  ~~Dr.M~~       0 unique,     0 total invalid heap argument(s)
  ~~Dr.M~~       0 unique,     0 total GDI usage error(s)
  ~~Dr.M~~       0 unique,     0 total handle leak(s)
  ~~Dr.M~~       0 unique,     0 total warning(s)
  ~~Dr.M~~       2 unique,     2 total,    150 byte(s) of leak(s)
  ~~Dr.M~~       0 unique,     0 total,      0 byte(s) of possible leak(s)
  ~~Dr.M~~ Details: C:\Users\jesse\AppData\Roaming\Dr. Memory\DrMemory-test.exe.4512.000\results.txt
  ~~Dr.M~~ WARNING: application exited with abnormal code 0xc0000005
  make: *** [runmem] Error 5
  </code></pre>
  
  <p>Also, when I print the "%s" of <code>this-&gt;words[0]</code> within the <code>add_word_to_arr</code> function, it is <code>null</code>.  Yet it can be printed from <code>print_words();</code>.</p>
  
  <p>The exe call is:</p>
  
  <pre><code>bin\test.exe a123 b123
  </code></pre>
  
  <p>The <code>init_arr()</code> is:</p>
  
  <pre><code>void XWORD::init_arr(int ilen, int iwid) {
      int y = 0;
      int x = 0;
      this-&gt;len = ilen;
      this-&gt;wid = iwid;
  
      this-&gt;arr = new char* [this-&gt;len];
      for (y=0; y&lt;(this-&gt;len); y++) {
          this-&gt;arr[y] = new char [1+this-&gt;wid];
          for (x=0; x&lt;(this-&gt;wid); x++) {
              this-&gt;arr[y][x] = BLANK;
          }
          this-&gt;arr[y][x] = (char) NULL;
      }
  }
  </code></pre>