user39320
user39320

Reputation: 53

why i get same values inside hash table?

I created hash table to insert my values . But when I insert more than one value I got same values inside all fields . my code is here :

create a structure for both user and hashtable

 struct UserNode
    {
    char *username;
    char *password;
    };

    struct HashTable
    {
        int size;
        struct UserNode *table;
    };

// initialize the hash table as NULL

      struct HashTable* initializeTable(int size)
       {

        struct HashTable *htable;
        int i = 0;
        if (size < MIN_TABLE_SIZE)
        {
                printf("Table Size Too Small\n");
                return NULL;
        }

        htable = malloc(sizeof(struct HashTable));

        if (htable == NULL)
        {
                printf("Out of Space\n");
                return NULL;
        }

        htable->size = size;

        htable->table = malloc(size * sizeof(struct UserNode));

        if (htable->table == NULL)
        {
                printf("Table Size Too Small\n");
                return NULL;
        }

        for (i = 0; i < htable->size; i++)
        {
                htable->table[i].username= malloc(20 * sizeof(char));
                htable->table[i].password = malloc(20 * sizeof(char));
                htable->table[i].username=NULL;
                htable->table[i].password=NULL;
        }

        printf("Hsh table sucessfully created\n");
        return htable;

       }

insert each user name to hash table

      int Insert(char *key,char *password,struct HashTable *htable)
     {
      int pos = 0;

      pos = Find(key,htable);
      printf("the value of key : %d\n",pos);
      if ((htable)->table[pos].username == NULL)
      {
        (htable)->table[pos].username,key ;
        (htable)->table[pos].password = password;
      }

      else
      {
                printf("Duplicate element ..\n");
      }
        return 0;
    }

This function to display the hash table

    void Retrieve(struct HashTable *htable)
     {

        int i=0;

     for (i = 0; i < htable->size; i++)
     {
        if (htable->table[i].username == NULL)

            printf("Position: %d \tusername : NULL\tpassword: NULL\n",i + 1);

        else

            printf("Position: %d \t username: %s\tpassword: %s\n",i + 1,htable->table[i].username,htable->table[i].password);

    }
    }

and I am calling these function from main function as :

............main codes............ ............................... case 1:

    printf("Enter size of the Hash Table:\n");
    scanf("%d",&size);
    htable = initializeTable(size);

   break;
   case 2:

    if (i > htable->size)

    {
        printf("Table is Full, Rehash the table\n");

        continue;
    }
         printf("Enter the username:\n");
         scanf("%s",&username);
         printf("Ebter the password:\n");
         scanf("%s",&password);
         Insert(username,password,htable);
         i++;

    break;


 case 3:
        printf("Display\n");
        Retrieve(htable);

    break;

But when I insert more username and password inside structures via insert function , I got same values in both fields . that is the new one is overwrite the previous one and display both value as new username . why ? an problem on my code ?

Upvotes: 0

Views: 123

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409196

Two things:

  1. You have the code:

    htable->table[i].username= malloc(20 * sizeof(char));
    htable->table[i].password = malloc(20 * sizeof(char));
    htable->table[i].username=NULL;
    htable->table[i].password=NULL;
    

    First you allocate memory, then you immediately overwrite the pointers with NULL making you loose the memory you just allocated, leading to a leak and possible undefined behavior (and probable crashes) if you use the pointers without checking for NULL.

  2. If you are going to allocate fixed sizes for the username and password members of the UserNode structure, why not make them arrays? Like

    struct UserNode
    {
        char username[20];
        char password[20];
    };
    

    This will, incidentally, also solve the first problem since you no longer need to allocate memory. It will also lessen memory fragmentation.

Upvotes: 1

timrau
timrau

Reputation: 23058

In Insert(),

 if ((htable)->table[pos].username == NULL)
  {
    (htable)->table[pos].username,key ;
    (htable)->table[pos].password = password;

The (htable)->table[pos].username,key; line just did nothing. Also, strings should be copied through strcpy() or strncpy(), not through pointer assignment.

So it should be:

if (htable->table[pos].username == NULL)
{
    htable->table[pos].username = malloc(strlen(key) + 1);
    strcpy(htable->table[pos].username, key);
    htable->table[pos].password = malloc(strlen(password) + 1);
    strcpy(htable->table[pos].password, password);
}

Upvotes: 0

Related Questions