A Safer STL Container Class

class CElement { unsigned int m_magic_number; ... CElement::CElement(const int index) : m_magic_number(0xfeedface), m_index(index) { } There are probably some other ways of doing it. For example, delete operator uses _BLOCK_TYPE_IS_VALID macro in debug mode. Either way, it is much safer to use for a little speed decrease. Note: In case your class is derived from some base class, you would have to use 4 bytes offset when dereferencing a pointer inside IsBlockValid function:
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.

Downloads

Download demo project - 5 Kb


Comments

  • Use auto_ptr

    Posted by Legacy on 11/26/2002 12:00am

    Originally posted by: Gandalf Hudlow

    map <int, auto_ptr<cMyClass>> m_mapData;

    m_mapData.insert(1, auto_ptr<cMyClass>(new cMyClass);

    auto pointers are great!

    Reply
  • So, you think that your new bad code will prevent you from coding - bad!?

    Posted by Legacy on 08/17/2002 12:00am

    Originally 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.

    Reply
  • An addition

    Posted by Legacy on 11/15/2001 12:00am

    Originally 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.

    Reply
  • What does 'feedface' mean?

    Posted by Legacy on 11/12/2001 12:00am

    Originally posted by: Yasuhiko Yoshimura

    Hi Alex,
    Thanks 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.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

  • Live Event Date: September 23, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT The cloud is not just about a runtime platform for your projects – now, you can do your development in the cloud, too. Check out this upcoming eseminar to learn how the cloud improves your development experience and team collaboration. Join Dana Singleterry, Principal Product Manager for Oracle Dev Tools, as he discusses how to simplify every aspect of the development lifecycle, including requirements gathering, version management, code …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date