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. CArray question

CArray question

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancequestion
7 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.
  • J Offline
    J Offline
    Jason Hihn
    wrote on last edited by
    #1

    I have objects that I create with new() and I store pointers to them in a CArray: Object *o; o=new Object(); Array.Add(o); When I want to delete all and free all memory, do I have to do: while (Array.GetSize()){ delete Array[0]; Array.RemoveAt(0); } Thanks!

    J L 3 Replies Last reply
    0
    • J Jason Hihn

      I have objects that I create with new() and I store pointers to them in a CArray: Object *o; o=new Object(); Array.Add(o); When I want to delete all and free all memory, do I have to do: while (Array.GetSize()){ delete Array[0]; Array.RemoveAt(0); } Thanks!

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Yep, that should work. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 Reply Last reply
      0
      • J Jason Hihn

        I have objects that I create with new() and I store pointers to them in a CArray: Object *o; o=new Object(); Array.Add(o); When I want to delete all and free all memory, do I have to do: while (Array.GetSize()){ delete Array[0]; Array.RemoveAt(0); } Thanks!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Jason Hihn wrote: while (Array.GetSize()){ delete Array[0]; Array.RemoveAt(0); } That's a pretty inefficient way of doing it since every time you remove at 0 the upper data has to be shifted down. I would just keep it simple e.g. for(int i=0; i

        1 Reply Last reply
        0
        • J Jason Hihn

          I have objects that I create with new() and I store pointers to them in a CArray: Object *o; o=new Object(); Array.Add(o); When I want to delete all and free all memory, do I have to do: while (Array.GetSize()){ delete Array[0]; Array.RemoveAt(0); } Thanks!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Jason Hihn wrote: while (Array.GetSize()){ delete Array[0]; Array.RemoveAt(0); } Let's try again. That's a pretty inefficient way of doing it since every time you remove at 0 the upper data has to be shifted down. I would just keep it simple e.g. for(int i=0; i < Array.GetSize(); i++) { delete Array[i]; } Array.RemoveAll(); Joel

          D 1 Reply Last reply
          0
          • L Lost User

            Jason Hihn wrote: while (Array.GetSize()){ delete Array[0]; Array.RemoveAt(0); } Let's try again. That's a pretty inefficient way of doing it since every time you remove at 0 the upper data has to be shifted down. I would just keep it simple e.g. for(int i=0; i < Array.GetSize(); i++) { delete Array[i]; } Array.RemoveAll(); Joel

            D Offline
            D Offline
            Dean Michaud
            wrote on last edited by
            #5

            Joel Matthias wrote: for(int i=0; i < Array.GetSize(); i++) { delete Array[i]; } Array.RemoveAll(); I prefer to write this as follows, personally: while (Array.GetSize()) { delete Array[0]; } Array.RemoveAll(); I don't like using for() loops when clearing out arrays - but I guess that's just me being picky about coding style. ;P : Dean 'Karnatos' Michaud

            L R 2 Replies Last reply
            0
            • D Dean Michaud

              Joel Matthias wrote: for(int i=0; i < Array.GetSize(); i++) { delete Array[i]; } Array.RemoveAll(); I prefer to write this as follows, personally: while (Array.GetSize()) { delete Array[0]; } Array.RemoveAll(); I don't like using for() loops when clearing out arrays - but I guess that's just me being picky about coding style. ;P : Dean 'Karnatos' Michaud

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              That's fine but the code doesn't work and infact it produces an infinite loop since you never actually remove the pointer from the array. Also you imply that 'while loops' are somehow more correct or are a better coding style than 'for loops' how do you justify that. People have been using 'for loops' to iterate through arrays for years. Joel

              1 Reply Last reply
              0
              • D Dean Michaud

                Joel Matthias wrote: for(int i=0; i < Array.GetSize(); i++) { delete Array[i]; } Array.RemoveAll(); I prefer to write this as follows, personally: while (Array.GetSize()) { delete Array[0]; } Array.RemoveAll(); I don't like using for() loops when clearing out arrays - but I guess that's just me being picky about coding style. ;P : Dean 'Karnatos' Michaud

                R Offline
                R Offline
                Ravi Bhavnani
                wrote on last edited by
                #7

                Dean `Karnatos` Michaud wrote: I prefer to write this as follows, personally:   while (Array.GetSize())   {     delete Array[0];   }   Array.RemoveAll(); Imho, that's bad, for 2 reasons: (1) it's inefficient to repeatedly make a function call to GetSize() and (2) it's dangerous to treat integers as boolean expressions. Better to write while (Arrary.GetSize() > 0). /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

                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