Deutsch   English   Français   Italiano  
<v2qnue$2evlu$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Malcolm McLean <malcolm.arthur.mclean@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: Good hash for pointers
Date: Fri, 24 May 2024 19:57:17 +0100
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <v2qnue$2evlu$1@dont-email.me>
References: <v2n88p$1nlcc$1@dont-email.me>
 <v2qm8m$2el55$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 May 2024 20:57:19 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b72444689a26009d2960a16da4ad5d91";
	logging-data="2588350"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/ormmdmwsYHV+ttSmjsd2f9yqd1dI/h4Q="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:SEjdOn6k4/jgtzMz/C7DHfgaeSE=
In-Reply-To: <v2qm8m$2el55$1@raubtier-asyl.eternal-september.org>
Content-Language: en-GB
Bytes: 2187

On 24/05/2024 19:28, Bonita Montero wrote:
> Am 23.05.2024 um 13:11 schrieb Malcolm McLean:
>>
>> What is a good hash function for pointers to use in portable ANSI C?
>>
>> The pointers are nodes of a tree, which are read only, and I want to 
>> associate read/write data with them. So potentially a lage number of 
>> pointers,and they might be consecutively ordered if they are taken 
>> from an array, or they might be returned from repeared calls to 
>> malloc() with small allocations. Obviously I have no control over 
>> pointer size or internal representation.
>>
> 
> Use FNV.
>

Here's an attempt.

/* FNV hash of a pointer */
static unsigned int hash(void *address)
{
     int i;
     unsigned long answer = 2166136261;
     unsigned char *byte = (unsigned char *) &address;

     for (i = 0; i < sizeof(void *); i++)
     {
         answer *= 16777619;
         answer ^= byte[i];
     }
     return (unsigned int) (answer & 0xFFFFFFFF);
}

Now what will compilers make of that?

-- 
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm