CArray question
-
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!
-
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!
Yep, that should work. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
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!
-
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!
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
-
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
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
-
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
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
-
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
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 writewhile (Arrary.GetSize() > 0)
. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com