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. Deleting STL list elements

Deleting STL list elements

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
13 Posts 5 Posters 0 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.
  • M Offline
    M Offline
    moredip
    wrote on last edited by
    #1

    Hi all, So I have a list< double* >, which contains arrays which were created with something like new double[4]. When I'm done with the list I need to free all of the elements. At first I tried this:

    list< double* > ::iterator it = my_list.begin();
    while( it != my_list.end() )
    delete [](*it);

    but that didn't work, with MFC saying the pointer was still valid, I think because the list still contained references to the element. So I figured the best way would be to walk thru the list, removing, then deleting, elements as I go. But I'm a little unsure about the behaviour of iterators in this situation. For example after the following code:

    list< double* > ::iterator my_it = my_list.begin();
    my_list.pop_front();

    Would my_it stil be valid? Where would it be pointing to? I need to figure out the quickest way to safely []delete all elements in the list. Any pointers? TIA, Pete

    M O J 3 Replies Last reply
    0
    • M moredip

      Hi all, So I have a list< double* >, which contains arrays which were created with something like new double[4]. When I'm done with the list I need to free all of the elements. At first I tried this:

      list< double* > ::iterator it = my_list.begin();
      while( it != my_list.end() )
      delete [](*it);

      but that didn't work, with MFC saying the pointer was still valid, I think because the list still contained references to the element. So I figured the best way would be to walk thru the list, removing, then deleting, elements as I go. But I'm a little unsure about the behaviour of iterators in this situation. For example after the following code:

      list< double* > ::iterator my_it = my_list.begin();
      my_list.pop_front();

      Would my_it stil be valid? Where would it be pointing to? I need to figure out the quickest way to safely []delete all elements in the list. Any pointers? TIA, Pete

      M Offline
      M Offline
      moredip
      wrote on last edited by
      #2

      Doh! this code: while( it != my_list.end() ) delete [](*it); should have been: while( it != my_list.end() ) delete [](*it++); I guess I'm too used to good old for loops :) well, I can now just run thru the list, deleting all the elements, then just delete the list. This is probably the quickest way.... right? Pete

      1 Reply Last reply
      0
      • M moredip

        Hi all, So I have a list< double* >, which contains arrays which were created with something like new double[4]. When I'm done with the list I need to free all of the elements. At first I tried this:

        list< double* > ::iterator it = my_list.begin();
        while( it != my_list.end() )
        delete [](*it);

        but that didn't work, with MFC saying the pointer was still valid, I think because the list still contained references to the element. So I figured the best way would be to walk thru the list, removing, then deleting, elements as I go. But I'm a little unsure about the behaviour of iterators in this situation. For example after the following code:

        list< double* > ::iterator my_it = my_list.begin();
        my_list.pop_front();

        Would my_it stil be valid? Where would it be pointing to? I need to figure out the quickest way to safely []delete all elements in the list. Any pointers? TIA, Pete

        O Offline
        O Offline
        Oz Ben Eliezer
        wrote on last edited by
        #3

        Try the following: list< double* >::iterator i; while ((i = my_list.begin()) != my_list.end()) { double *p = *i; my_list.pop_front(); delete [] p; }

        M 1 Reply Last reply
        0
        • O Oz Ben Eliezer

          Try the following: list< double* >::iterator i; while ((i = my_list.begin()) != my_list.end()) { double *p = *i; my_list.pop_front(); delete [] p; }

          M Offline
          M Offline
          moredip
          wrote on last edited by
          #4

          That looks like it would work, but I'm not sure if it's the quickest way to do it, calling begin() every time...

          1 Reply Last reply
          0
          • J jbarton

            I like to use something like: while ( ! my_list.empty() ) { delete [] my_list.front(); my_list.pop_front(); } Best regards, John

            M Offline
            M Offline
            moredip
            wrote on last edited by
            #5

            Yeah, that looks nice Do you think it'd be quicker this way... for( i = my_list.size(); i >= 0; i-- ) { delete [] my_list.front(); my_list.pop_front(); } Im guessing the lack of a function call would speed it up, what do you think? Pete

            J 1 Reply Last reply
            0
            • M moredip

              Hi all, So I have a list< double* >, which contains arrays which were created with something like new double[4]. When I'm done with the list I need to free all of the elements. At first I tried this:

              list< double* > ::iterator it = my_list.begin();
              while( it != my_list.end() )
              delete [](*it);

              but that didn't work, with MFC saying the pointer was still valid, I think because the list still contained references to the element. So I figured the best way would be to walk thru the list, removing, then deleting, elements as I go. But I'm a little unsure about the behaviour of iterators in this situation. For example after the following code:

              list< double* > ::iterator my_it = my_list.begin();
              my_list.pop_front();

              Would my_it stil be valid? Where would it be pointing to? I need to figure out the quickest way to safely []delete all elements in the list. Any pointers? TIA, Pete

              J Offline
              J Offline
              jbarton
              wrote on last edited by
              #6

              I like to use something like: while ( ! my_list.empty() ) { delete [] my_list.front(); my_list.pop_front(); } Best regards, John

              M 1 Reply Last reply
              0
              • M moredip

                Yeah, that looks nice Do you think it'd be quicker this way... for( i = my_list.size(); i >= 0; i-- ) { delete [] my_list.front(); my_list.pop_front(); } Im guessing the lack of a function call would speed it up, what do you think? Pete

                J Offline
                J Offline
                jbarton
                wrote on last edited by
                #7

                The speed difference would be insignificant. The members of list are generally inlined, so don't add any function call overhead. John

                M 1 Reply Last reply
                0
                • J jbarton

                  The speed difference would be insignificant. The members of list are generally inlined, so don't add any function call overhead. John

                  M Offline
                  M Offline
                  moredip
                  wrote on last edited by
                  #8

                  >>The speed difference would be insignificant. Whereas the time wasted while I try to understand my 'efficient' function in two weeks would be significant... I think I'll go with your version ;) Thanks John

                  J 1 Reply Last reply
                  0
                  • M moredip

                    >>The speed difference would be insignificant. Whereas the time wasted while I try to understand my 'efficient' function in two weeks would be significant... I think I'll go with your version ;) Thanks John

                    J Offline
                    J Offline
                    jbarton
                    wrote on last edited by
                    #9

                    My original reply was written assuming that you can't change the type of the list. If you can change the type of the list, I would do that instead. I would use: list< vector< double > > my_list. Then, when you erase any elements inside the list, the vector will automatically clean up after itself. John

                    M 1 Reply Last reply
                    0
                    • J jbarton

                      My original reply was written assuming that you can't change the type of the list. If you can change the type of the list, I would do that instead. I would use: list< vector< double > > my_list. Then, when you erase any elements inside the list, the vector will automatically clean up after itself. John

                      M Offline
                      M Offline
                      moredip
                      wrote on last edited by
                      #10

                      I _could_ change the type, but it would be more [bureaucratic] effort than it's worth. Thanks anyway, Pete

                      C R M 3 Replies Last reply
                      0
                      • M moredip

                        I _could_ change the type, but it would be more [bureaucratic] effort than it's worth. Thanks anyway, Pete

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #11

                        For future reference though, a vector of doubles in a list would have been a much nicer solution. I usually delete pointers if I need to carry them ( because of polymorphism ) using a for_each and a function object that calls delete on everything in my container. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002 Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002

                        1 Reply Last reply
                        0
                        • M moredip

                          I _could_ change the type, but it would be more [bureaucratic] effort than it's worth. Thanks anyway, Pete

                          R Offline
                          R Offline
                          Rama Krishna Vavilala
                          wrote on last edited by
                          #12

                          Even I don't see much sense in storing double* instead of double in a vector. It will also cause Heap fragmentation and your code may cause more page faults as each double may be scattered all over the memory. Unless you have very important reason against it, change it to double.

                          1 Reply Last reply
                          0
                          • M moredip

                            I _could_ change the type, but it would be more [bureaucratic] effort than it's worth. Thanks anyway, Pete

                            M Offline
                            M Offline
                            moredip
                            wrote on last edited by
                            #13

                            Well, it seems that it would be worth the effort, based on all of your posts. I shall get to it... Thanks for the advice everyone :)

                            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