Thread: Hash works in debug but not release

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Hash works in debug but not release

    Code:
    template <class T>
    unsigned int HashTable<T>::Hash(const std::string &name)
    {
        const char *pBuffer = name.c_str();
        unsigned int hashValue = 5381;
        int c;
    
        while (c = *pBuffer++)
        {
            hashValue = ((hashValue << 5) + hashValue) + c;
        }
    
        //Keep hash within range of table
        return hashValue % m_tableSize;
    }
    This hash works fine in debug mode and my objects are added with no problem. In release mode the hash is different on the same exact string and even though the hash table should be empty at this point the check to see if the hashtable is empty at the computed index fails.

    Very odd.

    EDIT:
    After changing some code to catch an exception for this later and throw up a message box indicating the computed hash the stupid thing started working even though I did not change any of the hash code.

    Twilight zone programming tonight.
    Last edited by VirtualAce; 01-19-2009 at 10:22 PM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Maybe the hash table is initialized to 0 or something in debug mode but not in release mode?

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I specifically init the entire table to 0 when I create it. I thought of the same thing but then saw my code that was always initializing it to zero.
    How can adding exception handling code just to output the hash to an ostringstream change the hash value from the function? It can't.
    I really think my compiler assembled the code incorrectly and when I recompiled it fixed it. Very weird. The hash should have been guranteed to work since the table was empty and therefore there would be no collisions.

    The only issue I can see with my code is that it is case sensitive. This could be fixed with a simple tolower(c). However this only makes the object easier to use since a programmer doesn't have to worry about the case of the string.
    Last edited by VirtualAce; 01-19-2009 at 10:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Group Project Help/Volunteer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 11:36 PM
  2. Compiled App as release - won't run - as debug runs
    By XenoCodex Admin in forum C++ Programming
    Replies: 7
    Last Post: 08-05-2002, 04:43 PM
  3. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM
  4. Why does this crash in release version but not debug?
    By Dual-Catfish in forum C++ Programming
    Replies: 1
    Last Post: 04-03-2002, 06:19 PM
  5. problem with MSVC++ 6's release mode
    By dirkduck in forum C++ Programming
    Replies: 13
    Last Post: 03-23-2002, 03:05 PM