title: How to delete an array of pointers
body: <p>I've been brushing up on my C++ as of late, and I have a quick question regarding the deletion of new'd memory. As you can see below i have a simple class that holds a list of FileData *. I created an array to hold the FileData objects to be pushed into the list. When ReportData is destructed I loop through the list and delete each element. My question is, how can i delete the array when I'm done using reportData, so that I do not have any memory leaks?</p>
  
  <h2>Report.h</h2>
  
  <pre><code>class REPORTAPI ReportData {
  public:
  
          ReportData()
          {
          }
  
          virtual ~ReportData()
          {
                  printf("Starting ReportData Delete\n");
                  for (list&lt;FileData*&gt;::iterator i = ReportFileData.begin(), e = ReportFileData.end(); i != e; )
                  {
                      list&lt;FileData*&gt;::iterator tmp(i++);
                      delete *tmp;
                      ReportFileData.erase(tmp);
                  }
  
                  for (list&lt;SupressionData*&gt;::iterator i = ReportSupressionData.begin(), e = ReportSupressionData.end(); i != e; )
                  {
                      list&lt;SupressionData*&gt;::iterator tmp(i++);
                      delete *tmp;
                      ReportSupressionData.erase(tmp);
                  }
  
                  ReportFileData.clear();
                  ReportSupressionData.clear();
  
                  printf("Finished ReportData Delete\n");
          }
  
          list&lt;FileData *&gt; ReportFileData;
          list&lt;SupressionData *&gt; ReportSupressionData;
  }
  
  
  extern "C" __declspec(dllexport) FileData* __stdcall createFileData(string fileName, long recordCount, long addPageCount)
  {
          return new FileData(fileName, recordCount, addPageCount);
  }
  </code></pre>
  
  <h2>Main.cpp</h2>
  
  <pre><code>        ReportData *reportData = createrd();
  
          if (reportData != NULL)
          {
                  CreateFileDataFunc createfd (reinterpret_cast&lt;CreateFileDataFunc&gt;(GetProcAddress (dll, "createFileData")));
  
                  const int num_files = 5;
                  FileData *fileData[num_files];
  
                  char buff[256] = {'\0'};
                  for (int i = 0; i &lt; num_files; i++)
                  {
                          sprintf(buff, "test: %d", i);
                          fileData[i] = createfd(buff, 1, 1);
                          reportData-&gt;ReportFileData.push_back(fileData[i]);
                  }
  
                  delete reportData;
                  reportData = NULL;
  
                  delete [] fileData; // this is throwing an access violation error:
                                      //EAccessViolation: 'Access violation at address 326025AF. Write of address 00000008'.
  
  
          }
  </code></pre>
  
  <p>--- I removed the delete oprations from the ReportData dtor
  and I'm now looping and deleting:</p>
  
  <pre><code>            for(int i = 0; i &lt; num_files; i++)
              {
                  delete fileData[i];
              }
  </code></pre>
  
  <p>This is easier to understand then having to rely on a separate object's dtor to clean up memory. </p>