Archived

This topic is now archived and is closed to further replies.

Spider_In_A_Web

Still having Linked List trouble

Recommended Posts

Like the Beatles said, HELP!!! I know i would be better of using STL, but i started this way and want to finish!! :-) The proplem i am having is that i keep getting ASSERT problems whenever i try to delete the pDead object i get ASSERT problems Here is the function!! void DeleteNode(CObject** pList, CObject** pDead) { //check pDead exists if(pDead) { //check for next objects CObject* pNext = (*pDead)->GetNext(); //set the next object to NULL (*pDead)->SetNext(NULL); //check the dead object is not the list head if((*pList) == (*pDead)) { //delete the object delete (*pDead); (*pDead) = NULL; //set the list head & return it (*pList) = pNext; } else { //temp pointer CObject* pTemp = (*pList)->GetNext(); //look for the dead objets preceding object while(pTemp != (*pDead) || pTemp == NULL) pTemp = pTemp->GetNext(); //delete the object delete (*pDead); (*pDead) = NULL; //set the preceding objects new Next pTemp->SetNext(pNext); //return the list head }putchar } } Any ideas???

Share this post


Link to post
Share on other sites
It seems to me you are making life unnecessarily complex. For instance, why are you using pointers to pointers? Here's what I would do:

    
void DeleteNode(CObject* pList, CObject* pDead)
{
//check pDead exists

if(pDead)
{
//check for next objects

CObject* pNext = pDead->GetNext();
//set the next object to NULL

pDead->SetNext(NULL);

//check the dead object is not the list head

if(pList == pDead)
{
//delete the object

delete pDead;
pDead = NULL;
//set the list head & return it

pList = pNext;
}
else
{
//temp pointer

CObject* pTemp = pList->GetNext();
CObject* pPrev = pList;
//look for the dead objets preceding object

while(pTemp != pDead)
{
pPrev = pTemp;
pTemp = pTemp->GetNext();
}
//delete the object

delete pDead;
pDead = NULL;
//set the preceding objects new Next

pPrev->SetNext(pNext);
//return the list head

}
}
}


Please note that is untested code, but I think it will work. If I'm right, you were deleting the object at *pDead and then setting that same piece of memory you just freed to NULL, which of course causes problems You also have no need of the pointers to pointers, which would be slower due to the extra differencing. If you need to pass a pointer to a pointer to the function, just difference it when you pass it (only once) as opposed to the many times in your original function. Another thing, if this is dealing with a class (CObject would seem to indicate a class), then this function might be better suited to being a class method rather than standalone. I hope that helps.

EDIT: I changed one pointer differencing I missed.

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Edited by - Yanroy on October 30, 2001 12:24:00 PM

Share this post


Link to post
Share on other sites
The reason it is not a class member function is beacuse i do not always have a first object in the list

this means i would not be able to run the function!!

I will give some serious thought to the long pointer thing tho, thanks!!

Share this post


Link to post
Share on other sites
just remebered why iam using a pointer to a pointer!!

The reason was because i could not get the dereferencing of the pointer to work when i was building the AddObject functions!!

The function would work fime but it would not affect the actual pointer that was passed in!!

Any suggestions!!

Ta.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Spider,

What actual line does the error occur on? Are you using Visual C++ from MS? You can step though the function and see where the actual assert happens? Is it when you are trying to delete the header?

Share this post


Link to post
Share on other sites
The error occurs on any call of delete!!

I think it is to do with inheritance but i cannot figure out what iam doing wrong

The CObject is a base class to several diffrent types (only one level of inheritance)

ps what are the code tags??

Share this post


Link to post
Share on other sites
The code tags used here to get the white background are [ source ] and [ /source ] without spaces.

assertions that occur during delete for MSVC, debug build are:
- damage before/after block: somewhere in your program you''ve overflowed or underflowed the memory buffer allocated to you and written to it.
- invalid block: what you''re trying to delete was never allocated to you. You''re probably re-deleting something you''ve already deleted, or you''re deleting an uninitialized pointer.

Share this post


Link to post
Share on other sites
Is the base class''s destructor virtual? For polymorphic (and I think inherited) classes that do data allocation, that''s a must.

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Share this post


Link to post
Share on other sites