Board index » cppbuilder » TList->Remove(void*)

TList->Remove(void*)

Does:

TList->Remove(void*)

actually free memory associated with the item pointed to in the list?
If so, how? How can it know the size with only a void pointer?

Dave

 

Re:TList->Remove(void*)


Dave,

All pointers stored in a TList are void*.  This means that it's up to the
programmer to decide what to store in the list and whether to destroy the
pointers when time to clean out the list.  TList actually doesn't free the
pointers it maintains, that is up to you.

In my library I have several global functions that are used to clean out
TList, TStrings, etc.  But, my functions assume TObject to be the base class
stored.  Since the destructor of TObject is virtual, it works in all cases
where the object being stored is a descendant of TObject.

So with this in mind ...

Quote
> actually free memory associated with the item pointed to in the list?

No.

Quote
> If so, how? How can it know the size with only a void pointer?

It doesn't care about the size of the object.  A pointer is always 32 bits
and this is all it cares to know about.  It doesn't free the pointer just
keeps it in the container.

- Clayton

Re:TList->Remove(void*)


Dave,

I forgot to mention what TList::Remove(void*) actually does.  It merely
removes the first occurrence of the pointer in the list.  So, if you stored
any NULL pointers in your list, it would remove the first one it found.

    TList*    mylist = new TList;

    mylist->Add(NULL);
    mylist->Add(someptr);
    mylist->Add(NULL);

    // here, mylist contains the following pointers:
    //    NULL, someptr, NULL

    mylist->Remove(NULL);    // Removes the first NULL
    mylist->Remove(NULL);    // Removes the second NULL

    // here, mylist contains the following pointer:
    //    someptr

- Clayton

Re:TList->Remove(void*)


In case you want a more general solution, Harold Howe has a splendid
article on typesafe and auto-freeing improvements to TList:
http://www.bcbdev.com/articles/list.htm

--
Timothy H. Buchman
========================================
City Center Theater
New York NY
tbuchmanPLEASE(at sign)REMOVEcitycenter.org
Please treat this signature information as confidential.
========================================
Search .borland newsgroup archives at:
http://www.mers.com/searchsite.html

Quote
Dave Keezer <dave.kee...@acterna.com> wrote in message

news:3a3fa174_1@dnews...
Quote
> Does:

> TList->Remove(void*)

> actually free memory associated with the item pointed to in the
list?
> If so, how? How can it know the size with only a void pointer?

Re:TList->Remove(void*)


No it doesn't free the memory....
you have to delete it yourself...

Quote
Dave Keezer <dave.kee...@acterna.com> wrote in message

news:3a3fa174_1@dnews...
Quote
> Does:

> TList->Remove(void*)

> actually free memory associated with the item pointed to in the list?
> If so, how? How can it know the size with only a void pointer?

> Dave

Other Threads