Linked List Cleanup
-
turns out cleaning up after using a linked list isn`t as simple as I thought. I know this is wrong but it`s the best code I was able to come up with. Any help for setting me on the correct path is appreciated.
void CleanUp(SomeNode * FrontTip)
{
SomeNode * Temp;Temp = (SomeNode\*)malloc(sizeof(SomeNode)); while(FrontTip->next != NULL) { Temp = FrontTip; FrontTip = Temp->next; free(Temp); }
}
-
turns out cleaning up after using a linked list isn`t as simple as I thought. I know this is wrong but it`s the best code I was able to come up with. Any help for setting me on the correct path is appreciated.
void CleanUp(SomeNode * FrontTip)
{
SomeNode * Temp;Temp = (SomeNode\*)malloc(sizeof(SomeNode)); while(FrontTip->next != NULL) { Temp = FrontTip; FrontTip = Temp->next; free(Temp); }
}
You don't need to
malloc
Temp
: it's just a pointer. You also forgot tofree
it. You also didn'tfree
theFrontTip
argument if it's the only thing allocated (i.e. if itsnext
isNULL
). I would writewhile(FrontTip != NULL)
{
SomeNode* Temp = FrontTip->next;
free(FrontTip);
FrontTip = Temp;
}Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
You don't need to
malloc
Temp
: it's just a pointer. You also forgot tofree
it. You also didn'tfree
theFrontTip
argument if it's the only thing allocated (i.e. if itsnext
isNULL
). I would writewhile(FrontTip != NULL)
{
SomeNode* Temp = FrontTip->next;
free(FrontTip);
FrontTip = Temp;
}Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.Quote:
You don't need to malloc Temp: it's just a pointer.
I couldn`t explain that bit myself either, it was probably serving for a different purpose in the article I was learning from. Thanks for your version and your help.
-
You don't need to
malloc
Temp
: it's just a pointer. You also forgot tofree
it. You also didn'tfree
theFrontTip
argument if it's the only thing allocated (i.e. if itsnext
isNULL
). I would writewhile(FrontTip != NULL)
{
SomeNode* Temp = FrontTip->next;
free(FrontTip);
FrontTip = Temp;
}Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.Hey Greg, there`s more than one issue in the function I posted. Like how do you go about managing a pointer that has been passed to the function. I`ve seen this code in a codeproject article:
void NukeA(A* p)
{
p->DoThis();
p->DoThat();delete p; // Kaboom! A dangling pointer is born p = nullptr;
}
My thought is that this is wrong because the original pointer (the pointer being passed) remains at the gates of the function unaffected by the changes inside NukeA();
void NukeSafelyA(A** p)
{
(*p)->DoThis();
(*p)->DoThat();
delete *p; // Nice
*p = nullptr;
}My question is how do you go about assigning a pointer to the pointer being passed or assign the pointer being passed to a new pointer that is stated/declared in the body of the function;
void NukeSafelyA(A** p)
{
(*p)->DoThis();
(*p)->DoThat();
A * Temp1;
A * Temp2;
(*p) = Temp1; //?
Temp2 = (*p); //?delete \*p; // Nice \*p = nullptr;
}
-
Hey Greg, there`s more than one issue in the function I posted. Like how do you go about managing a pointer that has been passed to the function. I`ve seen this code in a codeproject article:
void NukeA(A* p)
{
p->DoThis();
p->DoThat();delete p; // Kaboom! A dangling pointer is born p = nullptr;
}
My thought is that this is wrong because the original pointer (the pointer being passed) remains at the gates of the function unaffected by the changes inside NukeA();
void NukeSafelyA(A** p)
{
(*p)->DoThis();
(*p)->DoThat();
delete *p; // Nice
*p = nullptr;
}My question is how do you go about assigning a pointer to the pointer being passed or assign the pointer being passed to a new pointer that is stated/declared in the body of the function;
void NukeSafelyA(A** p)
{
(*p)->DoThis();
(*p)->DoThat();
A * Temp1;
A * Temp2;
(*p) = Temp1; //?
Temp2 = (*p); //?delete \*p; // Nice \*p = nullptr;
}
Setting a pointer to
nullptr
after invokingdelete
on it is definitely recommended. Or in C, setting it toNULL
after invokingfree
. To do it, pass the pointer by reference:void NukeSafelyA(A*& p)
{
p->DoThis();
p->DoThat();
delete p;
p = nullptr;
}Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Setting a pointer to
nullptr
after invokingdelete
on it is definitely recommended. Or in C, setting it toNULL
after invokingfree
. To do it, pass the pointer by reference:void NukeSafelyA(A*& p)
{
p->DoThis();
p->DoThat();
delete p;
p = nullptr;
}Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.Us old-school C programmers tend to forget about the existence of references. Well done, Greg. That's far cleaner than the pointer-to-pointer version the OP presented, but does exactly the same thing. To the OP: On the whole though, except as a learning exercise, and some really rare, odd cases, the STL should be the way to go. It's likely to be safer, perhaps faster, and certainly far better tested than your own implementation. The STL should be part of your basic tool kit for C++ programming.
Keep Calm and Carry On
-
Quote:
You don't need to malloc Temp: it's just a pointer.
I couldn`t explain that bit myself either, it was probably serving for a different purpose in the article I was learning from. Thanks for your version and your help.
To be specific about what @k5054 wrote, use
std::unique_ptr
[^] whenever possible. There's alsoshared_ptr
andweak_ptr
for a pointer with multiple owners (shared) or users (weak).Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Us old-school C programmers tend to forget about the existence of references. Well done, Greg. That's far cleaner than the pointer-to-pointer version the OP presented, but does exactly the same thing. To the OP: On the whole though, except as a learning exercise, and some really rare, odd cases, the STL should be the way to go. It's likely to be safer, perhaps faster, and certainly far better tested than your own implementation. The STL should be part of your basic tool kit for C++ programming.
Keep Calm and Carry On
Thanks guys. As you lean stuff, you begin to understand things that you previously heard about but never really understood. I`m taking the std pointers as a new horizon, at this point I feel that they are remote from where I am. I`ll be using the default c++ approach.