Hello, I've been working on a custom linked list for objects. The problem is I am deleting objects in the linked list, however it is having no effect on the memory usage.
I do not recieve any compiler, linker, or runtime errors. I don't recieve any errors relating to memory either.
Object class and associated functions:
I add objects like so:Code:class Obj { public: Obj(int iII=0) { pcNext=NULL; pcPrev=NULL; iI=iII; } ~Obj() { } //All static functions relate to the list itself; they can be accessed by typing Obj::NameOfFunc static Obj* listGetObj(int iIndex); static int listGetIndex(Obj* pcObj); static int listAdd(Obj* pcObj,int iWhere=-1); static bool listRemove(int iIndex); static void sayAll(); static Obj* pcStart; static Obj* pcEnd; static sui count; void sayI() { cout << iI << "\n"; } private: Obj* pcNext; Obj* pcPrev; int iI; }; sui Obj::count=0; Obj* Obj::pcStart=NULL; Obj* Obj::pcEnd=NULL; Obj* Obj::listGetObj(int iIndex) { if(iIndex<0||iIndex>=count) { return NULL; } else { Obj* pcTarg=pcStart; for(int i=0; i<=iIndex; i++) { if(i==iIndex&&pcTarg!=NULL) return pcTarg; else pcTarg=pcTarg->pcNext; } return pcTarg; } } int Obj::listGetIndex(Obj* pcObj) { return 0; } int Obj::listAdd(Obj* pcObj,int iWhere) { int index=iWhere; //If -1 set place to add to end of list. if(index<0) index=count; //If index is end of list if(index>=count) { if(pcEnd!=NULL) { pcEnd->pcNext=pcObj; pcObj->pcPrev=pcEnd; } pcEnd=pcObj; } //If index is start of list - keep in mind if list is empty the new item is both the start and end. if(index==0) { if(pcStart!=NULL) { pcStart->pcPrev=pcObj; pcObj->pcNext=pcStart; } pcStart=pcObj; } if(index!=0&&index<count) { Obj* pcMove=listGetObj(index); if(pcMove!=NULL) { pcObj->pcPrev=pcMove->pcPrev; pcObj->pcNext=pcMove; pcMove->pcPrev->pcNext=pcObj; pcMove->pcPrev=pcObj; } else { return -1; } } count++; return index; } bool Obj::listRemove(int iIndex) { //Get Object and Check Validity Obj* pcRemove=listGetObj(iIndex); if(pcRemove==NULL) return false; //If removing the first if(pcRemove==pcStart) { pcStart=pcRemove->pcNext; } //If removing the last if(pcRemove==pcEnd) { pcEnd=pcRemove->pcPrev; } //Bridge gaps caused removed link. Set the next link's last to last and the last link's next to next. if(pcRemove->pcNext!=NULL) { pcRemove->pcNext->pcPrev=pcRemove->pcPrev; } if(pcRemove->pcPrev!=NULL) { pcRemove->pcPrev->pcNext=pcRemove->pcNext; } delete pcRemove; count--; return true; } void Obj::sayAll() { for(Obj* pcCurrent=pcStart; pcCurrent!=NULL; pcCurrent=pcCurrent->pcNext) { pcCurrent->sayI(); } }
And remove them like so:Code:Obj::listAdd(new Obj(1));
Code://0 is the index, or distance from the start of the linked list. Obj::listRemove(0);



LinkBack URL
About LinkBacks


