Increasing array size

  • Thread starter Ioannis Hadjichambis
  • Start date
I

Ioannis Hadjichambis

I am trying to implement a hash-table and I would like to rehash it when
it starts to fill up. I am implementing the hash-table with an array
of linked lists. Now for me to increase the size of the hash-table I
must create a new array twice the size of the one before but the array
only accepts an imediate number eg array[1000] and will not accept a
variable eg array[count]. Does anyone know how to solve this problem?
thanks...
 
C

Christian Staudenmayer

I am trying to implement a hash-table and I would like to rehash it when
it starts to fill up. I am implementing the hash-table with an array
of linked lists. Now for me to increase the size of the hash-table I
must create a new array twice the size of the one before but the array
only accepts an imediate number eg array[1000] and will not accept a
variable eg array[count]. Does anyone know how to solve this problem?
thanks...

Like you would use a dynamic array of a regular type, i.e. using malloc().

Greetings, Chris
 
D

dandelion

Ioannis Hadjichambis said:
I am trying to implement a hash-table and I would like to rehash it when
it starts to fill up. I am implementing the hash-table with an array
of linked lists. Now for me to increase the size of the hash-table I
must create a new array twice the size of the one before but the array
only accepts an imediate number eg array[1000] and will not accept a
variable eg array[count]. Does anyone know how to solve this problem?
thanks...

Try allocating memory dynamically using malloc() instead of declaring a
fixed array. If your hash-table needs to grow, you can use realloc() to grow
the allocated dataspace or move it to a place where sufficient memory can be
allocated.
 
E

EventHelix.com

In C this would require using malloc for the array allocation.

It you were writing this code in C++, this would be an easy problem to
solve using the STL map.

Deepa
 
C

CBFalconer

Ioannis said:
I am trying to implement a hash-table and I would like to rehash it
when it starts to fill up. I am implementing the hash-table with an
array of linked lists. Now for me to increase the size of the
hash-table I must create a new array twice the size of the one
before but the array only accepts an imediate number eg array[1000]
and will not accept a variable eg array[count]. Does anyone know
how to solve this problem? thanks...

All done and available under GPL. See:

<http://cbfalconer.home.att.net/download/hashlib.zip>
 
J

James Dow Allen

"Require"? ... "Easy"? Let me rephrase Deepa's answers.

EventHelix.com said:
In C this would require using malloc for the array allocation.

"In C this is very simple to do with malloc()."
It you were writing this code in C++, this would be an easy problem to
solve using the STL map.

"In C++, many might do this by invoking an unneccessarily complicated
copyrighted library."

If you like C++, you're welcome to it (though not in this newsgroup)
but do let's not confuse the novice with misleading tone.

James
 
D

Default User

James Dow Allen wrote:
"In C++, many might do this by invoking an unneccessarily complicated
copyrighted library."


While this is not a C++ newsgroup, and hence this is off-topic, I'll
briefly note that map is (as of the 1998 standard) part of the C++
standard library.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top