Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9319009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:16:36+00:00 2026-06-19T03:16:36+00:00

I am creating an implementation of a hash table in C for educational purposes.

  • 0

I am creating an implementation of a hash table in C for educational purposes.

The hash function should return a size_t hash. Since the size of size_t is different in different platforms (and I want to use a hash function that hashes all the bits in a size_t), I thought of creating different hash functions for different sizes. As the hash function will be used as a function pointer, I suspect the compiler can’t inline code like this:

size_t hash4(void* key, size_t size);
size_t hash8(void* key, size_t size);

size_t hash(void* key, size_t size)
{
    if (sizeof(size_t) == 4)
    {
        return hash4(key, size);
    }
    else if (sizeof(size_t) == 8)
    {
        return hash8(ket, size);
    }
}

size_t (*hashFunc)(void* key, size_t size) = hash;

And two levels of indirection will be used each time the hash function will be called.

That’s why I thought of doing something like this: size_t (*hashFunc)(void* key, size_t size) = hash##sizeof(size_t); instead. Only one level of indirection will be used. The problem is that the sizeof operator isn’t available during the prepossessing phase.

So what would be a good way to define a preprocessor value which will expand to the correct size of size_t in each platform? I guess I could check against predefined macros, but I wonder if there’s a better way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:16:37+00:00Added an answer on June 19, 2026 at 3:16 am

    You could do this:

    size_t (*hashFunc)(void* key, size_t size) = (sizeof(size_t) == 8) ? hash8 : hash4;
    

    There’s also nothing wrong with eznme’s approach — write a single function that behaves differently according to the size of size_t. Provided that you don’t need the hash4 function for other purposes on 64bit implementations, of course.

    Regarding the title of the question — if you absolutely need to know about size_t at preprocessor time, then use the SIZE_MAX macro from stdint.h.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a UIView class . in the implementation there is a function
I understand the idea behind a hash table, but the implementation is getting frustrating.
I am creating several functions in which I want to return the interface instead
I was looking for a lock-free hash table implementation in C or C++ and
Are the old tricks (lookup table, approx functions) for creating faster implementations of sqrt()
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Is there a hash implementation around that doens't remember key values? I have to
Creating a basic ngram implementation in Python as a personal challenge. Started with unigrams
Im creating a custom listview implementation. This is an instance of my view declaried
I'm creating a custom implementation of the GWT Header<String> class for use on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.