473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Initialize the HashTable

The declaration is:
1
2 struct ListNode;
3 typedef struct ListNode *Position;
4 typedef Position List;
5 struct HashTbl;
6 typedef struct HashTbl *HashTable;
7
8 struct ListNode{
9 char name[4];
10 int GroupID;
11 Position Next;
12 };
13
14 struct HashTbl
15 {
16 int TableSize;
17 List *TheLists;
18 };
19
20 //The Initialization Function:
21
22 struct HashTbl *InitializeTable(int TableSize)
23 {
24 HashTable H;
25 int i;
26
27 H->TableSize=TableSize+3;
28
29 /* Allocate Table */
30 H=(HashTable)malloc(sizeof(struct HashTbl));
31 if(H == NULL)
32 fprintf(stderr,"Out of space!!!");
33
34 /* Allocate array of lists */
35 H->TheLists=(List *)malloc(sizeof(List)*H-
>TableSize);
36 if(H->TheLists == NULL)
37 fprintf(stderr,"Out of space!!!");
38
39 for(i = 0; i < H->TableSize; i++)
40 H->TheLists[i]=NULL;
41 return H;
42 }
43
44
Function Call:
HashTable AllThePersons= InitializeTable(1000*1000);

My idea is to Initialize the hash table with nodes of type (struct
ListNode *=List) pointers, distribute1000*1000 unit spaces to store
pointers.

The routine passed through the compiling but shut off with excepting
when running.

Could you help me?

Oct 18 '07 #1
1 7361
westlaker <we*******@126.comwrites:
The declaration is:
1
2 struct ListNode;
3 typedef struct ListNode *Position;
4 typedef Position List;
5 struct HashTbl;
6 typedef struct HashTbl *HashTable;
7
8 struct ListNode{
9 char name[4];
10 int GroupID;
11 Position Next;
I think lists are much clearer if you make the link explicit:
struct ListNode *Next;
Now everyone can see at a glance that this is a linked list.
12 };
13
14 struct HashTbl
15 {
16 int TableSize;
17 List *TheLists;
18 };
19
20 //The Initialization Function:
21
22 struct HashTbl *InitializeTable(int TableSize)
An unsigned type is often better for a size (in particular size_t).
If you use an int, you should check that the value it is not negative.
23 {
24 HashTable H;
25 int i;
26
27 H->TableSize=TableSize+3;
28
29 /* Allocate Table */
30 H=(HashTable)malloc(sizeof(struct HashTbl));
Too late. You have used H on line 27. The cast is not needed (and
may be a problem). The c.l.c idiom is:

H = malloc(sizeof *H);

(I had to check that HashTable is indeed a synonym for a pointer to a
struct HashTbl.)
31 if(H == NULL)
32 fprintf(stderr,"Out of space!!!");
Having found H to be NULL, it is not safe to just carry on!
33
34 /* Allocate array of lists */
35 H->TheLists=(List *)malloc(sizeof(List)*H->TableSize);
36 if(H->TheLists == NULL)
37 fprintf(stderr,"Out of space!!!");
Ditto.
38
39 for(i = 0; i < H->TableSize; i++)
40 H->TheLists[i]=NULL;
41 return H;
42 }
--
Ben.
Oct 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ante | last post by:
Hello Group. How can I create an array of Hashtables? I simply tried with Hashtable ht = new Hashtable; but when I put something into that array I get an nullpointer exception. ...
3
by: A. Burch | last post by:
I posted early about Hashtable use and the shallow/deep copy and storing a reference answered that question. I've got a larger implementation of this, but am not sure why I'm getting this error....
4
by: A.M-SG | last post by:
Hi, Considering the fact that we can initialize an two dimensional array like the following syntax: int myIntArray = new int { 1, 2, 3, 4, 5 }; Can I initialize a HashTable like an array? I...
3
by: MioTheGreat | last post by:
I know how to take a single hashtable, and then use a binaryformatter and a filestream to dump it to a file, but I need to serialize and deserialize a hashtable inside a class. I've been trying...
2
by: Mike | last post by:
I am trying to utilize Hashtable to facilitate mapping one value to another. I have successfully added several such pairs. When using the enumerators to find keys upon subsequent manipulation, I...
5
by: Victor Paraschiv | last post by:
I need to serialize into an XML file a hashtable. From MSDN at XmlSerializer:...
7
by: J L | last post by:
I have defined a structure private structure FieldInfo dim FieldName as string dim OrdinalPostioin as Integer dim DataType as Type dim Size as Integer end structure I read this information...
10
by: ibiza | last post by:
Hi, I have a class which will be used as a utility class (all its functions shared, no need to instantiate it, let's say just to calculate things) It needs a constant dictionary(of char,...
1
by: Big Bird | last post by:
System.Collections.Hashtable h = new System.Collections.Hashtable() { null }; Complains about ";" being expected after '()'. Using Visual Studio 2005. What's going on?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.