Link to home
Start Free TrialLog in
Avatar of dsch
dsch

asked on

deleting an array of pointers

Hi all.
I am having a Tad of grief trying to delete an array of string pointers.
Basicly, what it does is searches for several files and counts them. Then it creates an array to hold the number of files. Then it cycles through the array to set the size & place the name of the file.
I call the routine quite a lot and I have traced a memory leak to this.
How should I delete it? I have put what I've tried down below the code;

char **Files;
WIN32_FIND_DATA FoundData;
HANDLE FindHandle;
int filecount = 0;
FindHandle = FindFirstFile(
    "*.alm",     // pointer to name of file to search for
    &FoundData      // pointer to returned information
   );
if(INVALID_HANDLE_VALUE != FindHandle)
{
filecount++;
while(FindNextFile(

    FindHandle,     // handle to search
    &FoundData      // pointer to structure for data on found file
   ))
{
filecount++;
}
while(!FindClose( FindHandle/* file search handle*/ ))
{
}

Files = new char*[filecount];
FindHandle = FindFirstFile(
    "*.alm",     // pointer to name of file to search for
    &FoundData      // pointer to returned information
   );

if(INVALID_HANDLE_VALUE != FindHandle)
{
Files[0] = new char[strlen(FoundData.cFileName)+1];
strcpy(Files[0],FoundData.cFileName);
for(int i =1; i<filecount;i++)
{
FindNextFile(
    FindHandle,     // handle to search
    &FoundData      // pointer to structure for data on found file
        ) ;
Files[i] = new char[strlen(FoundData.cFileName)+1];
strcpy(Files[i],FoundData.cFileName);
}
}//end 2nd if(FindHandle)

}//end if(FindHandle)


/*delete attempt 1*/
delete []Files;

/*delete attemp 2*/
delete []*Files;

/*delete attemp 3*/
for(int i =0; i<filecount;i++)
{
        delete Files[i];
}
delete Files;


Thanks

David
Avatar of jkr
jkr
Flag of Germany image

Change

delete Files;

to read

delete[] Files;

If you want to delete a vector, you have to use "delete[]"
ASKER CERTIFIED SOLUTION
Avatar of fl0yd
fl0yd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dsch
dsch

ASKER

Thanks jkr but that was the same as my first attempt.

fl0yd:
Yes, I though option three was the correct way, I had just implemented it a bit wrong. Cheers.

I also verified this method in the Borland help file under delete which gave this example.
It may help others if I post it.
Thanks again
David

// ALLOCATE A TWO-DIMENSIONAL SPACE, INITIALIZE, AND DELETE IT.

#include <exception>
#include <iostream.h>

void display(long double **);
void de_allocate(long double **);

int m = 3;                               // THE NUMBER OF ROWS.
int n = 5;                               // THE NUMBER OF COLUMNS.

int main(void) {
   long double **data;

   try {                                 // TEST FOR EXCEPTIONS.
      data = new long double*[m];        // STEP 1: SET UP THE ROWS.
      for (int j = 0; j < m; j++)
          data[j] = new long double[n];  // STEP 2: SET UP THE COLUMNS

      }
   catch (std::bad_alloc) {  // ENTER THIS BLOCK ONLY IF bad_alloc IS THROWN.
      // YOU COULD REQUEST OTHER ACTIONS BEFORE TERMINATING
      cout << "Could not allocate. Bye ...";
      exit(-1);
      }

   for (int i = 0; i < m; i++)
      for (int j = 0; j < n; j++)
          data[i][j] = i + j;            // ARBITRARY INITIALIZATION

   display(data);
   de_allocate(data);
   return 0;
   }

void display(long double **data) {
   for (int i = 0; i < m; i++) {

        for (int j = 0; j < n; j++)
             cout << data[i][j] << " ";
       cout << "\n" << endl;
       }
   }

void de_allocate(long double **data) {
   for (int i = 0; i < m;  i++)
       delete[] data[i];                 // STEP 1: DELETE THE COLUMNS

   delete[] data;                        // STEP 2: DELETE THE ROWS

   }