A Safer STL Container Class
Posted
by Alex Kravchenko
on November 1st, 2000
return (*((unsigned int*)((int)pBlock + 4)) == 0xfeedface);I also added some functions to make it MFC like: GetFirst(), GetNext(), RemoveLastRead(), etc. In the sample code I added 10 elements into the map. Then I deleted elements 5 and 7 outside the map. Then I looped through displaying the content. Notice that after going through all the elements CTidyMap reports 8 to be the actual size.
Example Usage
CTidyMap<CElement, int> mymap;
CElement* pElement[10];
for(int i = 0; i < 10; i++) {
pElement[i] = new CElement(i);
mymap.Add(pElement[i], i);
}
cout << "Number of elements in the map: "
<< mymap.GetCount() << endl;
delete pElement[5];
cout << "Deleted Element number 5" << endl;
delete pElement[7];
cout << "Deleted Element number 7" << endl;
cout << "Still does not know about elements being deleted, size: "
<< mymap.GetCount() << endl;
CElement* pExElem = mymap.GetFirst();
while(pExElem) {
cout << "Found Element number: "
<< pExElem->GetIndex()
<< endl;
pExElem = mymap.GetNext();
}
cout << "Now the number of elements in the map: "
<< mymap.GetCount()
<< endl;
Output
Number of elements in the map: 10 Deleted Element number 5 Deleted Element number 7 Still does not know about elements being deleted, size: 10 Found Element number: 0 Found Element number: 1 Found Element number: 2 Found Element number: 3 Found Element number: 4 Found Element number: 6 Found Element number: 8 Found Element number: 9 Now the number of elements in the map: 8
Bonus
I also made the feedface optional so if you initialize it like this:CTidyMap<CElement, int> mymap = false;you can watch the program crash.

Comments
Use auto_ptr
Posted by Legacy on 11/26/2002 12:00amOriginally posted by: Gandalf Hudlow
map <int, auto_ptr<cMyClass>> m_mapData;
m_mapData.insert(1, auto_ptr<cMyClass>(new cMyClass);
auto pointers are great!
ReplySo, you think that your new bad code will prevent you from coding - bad!?
Posted by Legacy on 08/17/2002 12:00amOriginally posted by: JK
This is a hack - and a hack is NEVER a good solution! Let's say I use a list of pointers - should I wrap stl's list class also? What if I use a plain array of pointers - should I create an array class just for being SURE that it's OK for me to delete the object and forget about the pointer stored in that array?
It would be a lot cleaner if you would just take care of deleting the object and removing the pointer from map. And I'm sure you can find a much better way of removing the pointer from the map automatically when deleting the object.
Anyway, you still have useless allocated memory for the map nodes that hold invalid pointers. What if I have 100000 invalid pointers STORED in a map - isn't that a waste of memory - and searching performance?
Btw. What if I have 10 threads and each one of them wants to iterate through the map at some point (just theory)?
Just code your programs bug free. Don't try to hack out your own bugs - DE-bug them.
ReplyAn addition
Posted by Legacy on 11/15/2001 12:00amOriginally posted by: Yasuhiko Yoshimura
Today I have implemented this idea to my ATL test project and have found a fact that troubled me.
It stands on 'virtual function table' of class object.
I made destructor of my class corresponding to 'class CElement' to 'virtual' one.
This idea set value of the pointer of my class object with its vftable pointer.
Then the magic number, 0xfeedface was stored at the second pointer neighbor of the above.
And caused 'IsBlockValid(...)' always returned false like the following dump image.
02874D28: 3C 6C 6D 02 CE FA ED FE
vftable magic_number
Thanks.
ReplyWhat does 'feedface' mean?
Posted by Legacy on 11/12/2001 12:00amOriginally posted by: Yasuhiko Yoshimura
Hi Alex,
ReplyThanks for your good works and expalnation.
I would like to know what 'feedface' does suggest.
Such as 'A feeder's face' or 'A face fed and saticefied'.
I think it stands for VC++'s debug values on memory.
The following article is written in '\VC98\crt\src\dbheap.c'.
------------------------------------------------------------
/* fill no-man's land with this */
static unsigned char _bNoMansLandFill = 0xFD;
/* fill free objects with this */
static unsigned char _bDeadLandFill = 0xDD;
/* fill new objects with this */
static unsigned char _bCleanLandFill = 0xCD;
------------------------------------------------------------
Thanks.