Solved

cannot get the hash function in c to work

Posted on 2004-10-08
7
219 Views
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;
}IPAddress;
typedef struct bucket
{
  int           size;
  ListPtr       listPtr;
}Bucket;
typedef struct conListNode
{
  IPAddress     ipAddress;
  int           portNumber;
  ListNodePtr   next;
}ConListNode;
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;
}connctnList;

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)) % \
               conList->tableSize_N;
  return hashValue;
}

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

Thanks a million from a very confused person.
0
Comment
Question by:kwy
  • 4
  • 2
7 Comments
 
LVL 11

Expert Comment

by:pratap_r
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));
*b=conList;
......
.....
}

main()
{
      connctnList *conList;
      makeConnectionList(&conList);
      ipaddr ip;
      ip.digit1=127;ip.digit2=0;ip.digit3=0;ip.digit4=1;
      addConnection(conList,ip,100);
}

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

enjoy!
pratap
0
 
LVL 11

Expert Comment

by:pratap_r
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
0
 

Author Comment

by:kwy
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;
  -------------etc-----------------
 if (ParseLine(line, &tempCommand, tempIPAddress, tempPortOrIP) == FALSE)
    {      printf("\nError on Line Number %d in file %s", lineNum, argv[FILENAME]);    }
 else
 {
      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 !!");       }
      }
     -----etc

Thanks again.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Expert Comment

by:suryaxchange
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
Surya.
0
 
LVL 11

Accepted Solution

by:
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...

enjoy!
Pratap
0
 

Author Comment

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

Expert Comment

by:pratap_r
ID: 12265604
with pleasure :-)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now