problem retaining data

This is a discussion on problem retaining data within the C++ Programming forums, part of the General Programming Boards category; A few days ago i posted about my hash table template.. i given up on it to make smaller functions ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123

    problem retaining data

    A few days ago i posted about my hash table template.. i given up on it to make smaller functions which were easier, and it compiled with no problem. The problem I'm having is after testing the program with two elements into the hash table, the third additional element bugs the data structure.

    Code:
    int HashTable_insert( const char * hash_key, void * object, _instant_hash * entry_array)
    {
        
        unsigned int h = SuperFastHash(hash_key, strlen(hash_key)) % entry_array->size;
        
        if(entry_array->table[h] == NULL)
      {  
         struct _node * Cur = (_node *)malloc(sizeof(_node));
         Cur->object = object;
         Cur->key = hash_key;
         entry_array->table[h] = Cur;
         return 0;
      }
      else
      {
        struct _node * it;
    	 
    
    	  while(entry_array->cursor < entry_array->size && entry_array->table[entry_array->cursor] != NULL)
    		  ++entry_array->cursor;
    	  if( entry_array->cursor == entry_array->size)
    		  return 1;
    
    	  struct _node * Cur = (_node *)malloc(sizeof(_node));
          Cur->object = object;
          Cur->key = hash_key;
          entry_array->table[entry_array->cursor] = Cur;
    	  it = entry_array->table[h];
    
    	  while(it->next != NULL)
    		  it = it->next;
    
    	  it->next = entry_array->table[entry_array->cursor];
         return 0;
      }  
      
      return 1;
    }
    Attached Files Attached Files
    Last edited by Darkinyuasha1; 07-21-2009 at 02:25 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    I can't even get your source to compile...
    How about you make all files available?
    And why do you have several definitions of the same function?
    Is instant_hash supposed to be C code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    Quote Originally Posted by Elysia View Post
    I can't even get your source to compile...
    How about you make all files available?
    And why do you have several definitions of the same function?
    Is instant_hash supposed to be C code?
    The instant_hash is indeed in C idk what ur talking about same definition(unless i did something wrong then clue me in) but I'll post all the source code
    Attached Files Attached Files

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    instant_hash.c defines SuperFastHash, but so does does superfasthash.h! Two of the same!
    instant_hash.c also includes instant_hash.h, but that file isn't even attached here!
    superfasthash.h declared the struct _instant_hash (which is a bad name, btw, since names beginning with _ is reserved for the compilers), which instant_hash.c uses, but it uses a member table, among others, which isn't declared in the struct in the first place!

    This code is a mess and won't compile.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That doesn't even compile. Your header file doesn't match up with the implementation, either - there are a lot of inconsistencies (in fact you include instant_hash.h but provided superfasthash.h). At this point I'd recommmend putting every thing in a single file, and don't even bother forward declaring anything unless you have to. Once you get everything to compile repost the entire code.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    ooh nvm i made a mistake lol.... i sent the wrong header file .. im very sorry wasn't paying attention hahaa
    disregard the superfast hash file
    Attached Files Attached Files
    Last edited by Darkinyuasha1; 07-21-2009 at 03:57 PM.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, be careful when declaring a class/struct member as a pointer. It should always be clear who owns what and you can't simply set a pointer to some random address and expect things to work out. So for example, the node_ class should really be using an std::string in place of a raw pointer.


    Code:
    typedef struct _instant_hashtable_t
    {
      struct _node **table;  
      size_t size;
      size_t cursor;
    }_instant_hash;
    For C++, you can simply do this:

    Code:
    struct _instant_hash
    {
      _node **table;  
      size_t size;
      size_t cursor;
    };
    Suggestion:

    Code:
    int HashTable_insert( const char * hash_key, void * object, _instant_hash * entry_array)
    {
        
        unsigned int h = SuperFastHash(hash_key, strlen(hash_key)) % entry_array->size;
        
        if(entry_array->table[h] == NULL)
      {  
         struct _node * Cur = (_node *)malloc(sizeof(_node));
         Cur->object = object;
         Cur->key = (char*)malloc(strlen(hash_key) + 1);
         Cur->next = NULL;
         strcpy(Cur->key, hash_key);
         entry_array->table[h] = Cur;
         return 0;
      }
      else
      {
        _node * it = entry_array->table[h];
    	while(it->next)
    	{
    		if(strcmp(it->key, hash_key) == 0)
    		{
    			// if the object already exists, return 1.
    			// otherwise, reset object and return 0.
    		}	
    		it = it->next;
    	}
    	  struct _node * Cur = (_node *)malloc(sizeof(_node));
          Cur->object = object;
          Cur->key = (char*)malloc(strlen(hash_key) + 1);
          strcpy(Cur->key, hash_key);
          Cur->next = NULL;
          it->next = Cur;
         return 0;
      }  
      
      return 1;
    }
    
    void*  HashTable_search(const char * hash_key, _instant_hash *entry_array)
    {
           unsigned int h = SuperFastHash(hash_key,strlen(hash_key)) % entry_array->size;
           
           if ( entry_array->table[h] != NULL ) 
      {
        struct _node *it;
     
        /* Search the chain at index h */
        for ( it = entry_array->table[h]; it != NULL; it = it->next )
        {
          if ( strcmp(it->key, hash_key) == 0 )
            return it->object;
        }
      }
     return NULL;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    thank you, for the help I'll put in mind switching from char raw pointer.. and I'll take your suggestion deeply appreciate!!! =D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. problem with data in allocated memory
    By supi in forum C Programming
    Replies: 3
    Last Post: 06-09-2008, 02:06 AM
  3. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. Replies: 4
    Last Post: 06-14-2005, 05:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21