Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Linked List Cleanup

Linked List Cleanup

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelp
8 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    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);
    }
    

    }

    Greg UtasG 1 Reply Last reply
    0
    • C Calin Negru

      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);
      }
      

      }

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      You don't need to malloc Temp: it's just a pointer. You also forgot to free it. You also didn't free the FrontTip argument if it's the only thing allocated (i.e. if its next is NULL). I would write

      while(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.

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      C 2 Replies Last reply
      0
      • Greg UtasG Greg Utas

        You don't need to malloc Temp: it's just a pointer. You also forgot to free it. You also didn't free the FrontTip argument if it's the only thing allocated (i.e. if its next is NULL). I would write

        while(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.

        C Offline
        C Offline
        Calin Negru
        wrote on last edited by
        #3

        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.

        Greg UtasG 1 Reply Last reply
        0
        • Greg UtasG Greg Utas

          You don't need to malloc Temp: it's just a pointer. You also forgot to free it. You also didn't free the FrontTip argument if it's the only thing allocated (i.e. if its next is NULL). I would write

          while(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.

          C Offline
          C Offline
          Calin Negru
          wrote on last edited by
          #4

          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;
          

          }

          Greg UtasG 1 Reply Last reply
          0
          • C Calin Negru

            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;
            

            }

            Greg UtasG Offline
            Greg UtasG Offline
            Greg Utas
            wrote on last edited by
            #5

            Setting a pointer to nullptr after invoking delete on it is definitely recommended. Or in C, setting it to NULL after invoking free. 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.

            <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
            <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

            K 1 Reply Last reply
            0
            • Greg UtasG Greg Utas

              Setting a pointer to nullptr after invoking delete on it is definitely recommended. Or in C, setting it to NULL after invoking free. 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.

              K Offline
              K Offline
              k5054
              wrote on last edited by
              #6

              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

              C 1 Reply Last reply
              0
              • C Calin Negru

                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.

                Greg UtasG Offline
                Greg UtasG Offline
                Greg Utas
                wrote on last edited by
                #7

                To be specific about what @k5054 wrote, use std::unique_ptr[^] whenever possible. There's also shared_ptr and weak_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.

                <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
                <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

                1 Reply Last reply
                0
                • K k5054

                  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

                  C Offline
                  C Offline
                  Calin Negru
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups