Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,309 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 859 people online right now. Registration is fast and FREE... Join Now!




How to properly delete this array?

 
Reply to this topicStart new topic

How to properly delete this array?

I_want_to_learn
20 Dec, 2008 - 12:47 PM
Post #1

New D.I.C Head
*

Joined: 27 Aug, 2008
Posts: 30

I have an array allocated to take 1000 strings.

How can i delete this arrays?

This is my code:

CODE
void CFileCleanDlg::Prefetch(void)
{
//giva all allocated arrays a value
for(int n = 0;n <= 1000;n++)
{
        PathArray[n] = "";
        SearchArray[n] = "";
}
//search for all folders in C drive
SearchDir("C:\\");

}

void CFileCleanDlg::SearchDir(char* Dir)
{
//variables
CString Path = Dir;
CString SPath = Dir;
CString FileName;
char Directory[MAX_PATH];
bool Search = true;
int Array = Arraybk;
SPath += "*";

//search for the first file/folder
FFind = FindFirstFile((LPCTSTR)SPath,&FindFileData);
//if its a folder continue
if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
//if the folder is NOT . or .. continue
if(IsDot(FindFileData.cFileName) == FALSE)
{
//search in the array to check if we already searched in this folder
if(NewPath(FindFileData.cFileName,Arraybk) == TRUE)
{
//Assign path with the full search path ex:C:\\Program Files\\Whatever\\*
Path += FindFileData.cFileName;
SPath = Path;
Path += "\\";
//add found folders to the listBox
m_List.InsertItem(0,Path);
m_List.Update(0);
//convert CString to char[256]
sprintf_s(Directory,"%s",Path);
//Allocate memory for the array
PathArray[Arraybk] = new char[strlen(FindFileData.cFileName)+1];
//Add the folders name to the folder name array to later check if we already serched on it
strcpy(PathArray[Arraybk],FindFileData.cFileName);
//Allocate memory for the 2nd array
SearchArray[Arraybk] = new char[strlen(Directory)+1];
//Add the full serach path to the path array to later use it to move backwards to the last folder.
strcpy(SearchArray[Arraybk],Directory);
//self explanatory
Arraybk++;
Array = Arraybk;
//call this function again with the new full serach path
SearchDir(Directory);
}
}
}
//find next file/folder
while(FindNextFile(FFind,&FindFileData))
{
//if is a folder continue
if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
//if it is NOT . or .. continue
if(IsDot(FindFileData.cFileName) == FALSE)
{
//search in the array to check if we already searched in this folder
if(NewPath(FindFileData.cFileName,Arraybk) == TRUE)
{
//Assign path with the full search path ex:C:\\Program Files\\Whatever\\*
Path += FindFileData.cFileName;
SPath = Path;
Path += "\\";
//inserts folder name in listBox
m_List.InsertItem(0,Path);
m_List.Update(0);
//convert CString to char[256]
sprintf_s(Directory,"%s",Path);
//Allocate memory for the array
PathArray[Arraybk] = new char[strlen(FindFileData.cFileName)+1];
//Add the folders name to the folder name array to later check if we already serched on it
strcpy(PathArray[Arraybk],FindFileData.cFileName);
//Allocate memory for the array
SearchArray[Arraybk] = new char[strlen(Directory)+1];
//Add the full serach path to the path array to later use it to move backwards to the last folder.
strcpy(SearchArray[Arraybk],Directory);
//self explanatory
Arraybk++;
Array = Arraybk;
//call this function again with the new full serach path
SearchDir(Directory);
}
}
}
}
//finish the serach and delete the search handle
FindClose(FFind);
}

bool CFileCleanDlg::IsDot(char* str)
{
//check if the found folder is not . or ..
if(strcmp(str,".") && strcmp(str,"..") != 0)
return FALSE;
//else
return TRUE;
}

bool CFileCleanDlg::NewPath(char* str,int Array)
{
    //search the whole array checking if the folder name someware in it
    for(int n = 0;n <= 1000;n++)
    {
    //compare array strings to the folder name
    if(strcmp(str,PathArray[n]) == 0)
    {
    //if found the name then we already searched in that folder
    return FALSE;
    }
    }
    //if not then add the folder name to the array and keep searching
    return TRUE;
}


Hope is documented enogh

If i don't delete the array i get this msg

IPB Image

and if i do delete it,it gives me this msg

IPB Image

I know all the problems i'm having is because of the array.

User is offlineProfile CardPM
+Quote Post


JackOfAllTrades
RE: How To Properly Delete This Array?
20 Dec, 2008 - 01:59 PM
Post #2

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 3,055



Thanked: 270 times
Dream Kudos: 50
Expert In: Nothing. Well, nothing relevant here anyway. ;)

My Contributions
Go through each element of the array and call delete on each dynamically-allocated string. If the array is dynamically allocated as well, then call delete [] PathArray; after you've deleted its contents.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 07:17PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month