cannot get the hash function in c to work

Posted on 2004-10-08
Last Modified: 2010-04-17
Hi experts, I'm doing an assignment on hash tables and require some advise

I have the following structs:

typedef struct bucket *ListPtr;
typedef struct conListNode *ListNodePtr;
typedef struct ipaddr
  int digit1, digit2, digit3, digit4;
typedef struct bucket
  int           size;
  ListPtr       listPtr;
typedef struct conListNode
  IPAddress     ipAddress;
  int           portNumber;
  ListNodePtr   next;
typedef ListPtr BucketPtr;
typedef struct connctnList
  int         tableSize_N;
  int         bucketThreshold_m;
  int         primeNo1_k1;
  int         primeNo2_k2;
  int         primeNo3_k3;
  int         primeNo4_k4;
  int         primeNo5_k5;
  BucketPtr   *bucketPtr;

To initialise the table, I have the following function:

int makeConnectionList(connctnList *conList)
  int counter;
  conList = malloc(sizeof(connctnList));
  if(conList == NULL)  {    return FAILURE;  }

  conList->tableSize_N = TABLE_SIZE;
  conList->bucketThreshold_m = INITIALISE;
  conList->primeNo1_k1 = K1;
  conList->primeNo2_k2 = K2;
  conList->primeNo3_k3 = K3;
  conList->primeNo4_k4 = K4;
  conList->primeNo5_k5 = K5;

  conList->bucketPtr = malloc(sizeof(Bucket) * conList->tableSize_N);

  if (conList->bucketPtr == NULL)  {    return FAILURE; }

  for (counter = 0; counter < conList->tableSize_N; counter++)
    conList->bucketPtr[counter] = malloc(sizeof(Bucket));
    if(conList->bucketPtr[counter] == NULL)    {
      return FAILURE;
    else    {
      conList->bucketPtr[counter]->listPtr = NULL;
  return SUCCESS;

This function to call the hash function to calculate position within array is:

int addConnection(connctnList *conList, IPAddress address, int port)
  int hashValue = INITIALISE;
  hashValue = hashCalculation (conList, address, port);
  printf("%d\n", hashValue);
  return 0;

And this is the hash function:

int hashCalculation(connctnList *conList, IPAddress address, int port)
   int hashValue = INITIALISE;

  hashValue = ((conList->primeNo1_k1 * address.digit1) + \
              (conList->primeNo2_k2 * address.digit2) + \
              (conList->primeNo3_k3 * address.digit3) + \
              (conList->primeNo4_k4 * address.digit4) + \
              (conList->primeNo5_k5 * port)) % \
  return hashValue;

Can someone please explain to me why the hashValue printed out is rubbish.

Thanks a million from a very confused person.
Question by:kwy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 11

Expert Comment

ID: 12260227
can you post the code from where you are calling makeConnectionList ??

as far as i can tell your connection object is not passed properly

try changing to
int makeConnectionList(connctnList **b)
connctnList *conList;
conList = (connctnList*)malloc(sizeof(connctnList));

      connctnList *conList;
      ipaddr ip;

using a table size of 100 i got the hash as 61 --> seems to work!!?

LVL 11

Expert Comment

ID: 12260241
oh and i used 2,3,5,7,11 as values for K1, k2, k3, k4, k5 since i dint know what values you were using.. but i dont see any problem with that

Author Comment

ID: 12263371
Hi pratap

Because this is an assignment, I cannot change the function parameters.  Please see below the extracts from main where the functions are called, your advise is much appreciated :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "connctnList_HT.h"
#include "connctnList.h"

int main (int argc, char **argv)
  connctnList conList;

  IPAddress ipAddress1;
  IPAddress ipAddress2;
 if (ParseLine(line, &tempCommand, tempIPAddress, tempPortOrIP) == FALSE)
    {      printf("\nError on Line Number %d in file %s", lineNum, argv[FILENAME]);    }
      if (tempCommand == 'C')
        if (makeConnectionList(&conList) == FALSE)
        {         FatalError("Unable to create list !!");        }
      else if (tempCommand == 'A')
        sscanf (tempPortOrIP, "%d", &port);

        if (ParseIP(tempIPAddress, &ipAddress1) == FALSE)
        {     printf("Unable to create IPAddress structure");        }
        if (addConnection(&conList, ipAddress1, port) == FALSE)
        {     FatalError("Unable to add node !!");       }

Thanks again.
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.


Expert Comment

ID: 12265264
In the following declararation,
  int makeConnectionList(connctnList *conList)
connctnList *conList is wrong.

Use connctnList **conList, because you are allocating memory for conList in the function i.e., you are modifying the pointer so, you need to send the pointer to this pointer same as you send address of integer to modify the value of that integer.

Try this you will get succeeded
LVL 11

Accepted Solution

pratap_r earned 500 total points
ID: 12265449
yup thats exactly what you need to do. that is what i had mentioned in my reply .. change the * to ** in input parameter :-)

in your main function you will have to change the declaration of connctnList conList; to connctnList *conList; because you are allocating memory in your make connection function. but when you declare connctnList conList; your memory is already allocated! if you are not allowed to change your parameter list then you should remove the malloc inside your makeconnection function..

your new make connection will be
int makeConnectionList(connctnList *conList)
  int counter;
  if(conList == NULL)  {    return FAILURE;  }
  conList->tableSize_N = TABLE_SIZE;
nothing else needs to be changed... :-)  rest of the code is fine. the malloc was basically allocating a memory but your main's conlist was not getting it properly...


Author Comment

ID: 12265515
Thanks guys for your help, especially you Pratap.
The program is now working beautifully!
LVL 11

Expert Comment

ID: 12265604
with pleasure :-)

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Introduction to Processes

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question