News for hackers. Stuff that matters.
Re:Show HN: A portable hash map in C by zabzonk on Sunday December 08, 2024 @04:19PM (#42360571)
Purely out of interest, and probably my bad, but what is:

    (void)k_sz;
doing? I've seen fussy people doing:

     (void) printf( "hello" );
but not what you have written. As I say, probably ignorance on my part.
Re:Show HN: A portable hash map in C by Sesse__ on Sunday December 08, 2024 @04:26PM (#42360655)
It's an idiom for telling the compiler “don't give a warning on this argument not being used, I know about it”. Not all compilers will heed it. Other choices include e.g. __attribute__((unused)) (GNU), [[maybe_unused]] (C++17/C23) or just not giving it a name (nonstandard, but very commonly supported).
Re:Show HN: A portable hash map in C by tredre3 on Sunday December 08, 2024 @04:34PM (#42360732)
> or just not giving it a name (nonstandard, but very commonly supported)

It's only supported by gcc 11+ and clang 11+ (both from ~2021) because it was a suggested C2x extension. It's been accepted in C23 so other compilers will eventually support it, but I wouldn't hold my breath regarding when that happens.

Re:Show HN: A portable hash map in C by Sesse__ on Sunday December 08, 2024 @04:37PM (#42360764)
Ah, of course, in C mode it's different. (I nearly always use C++ mode, where at least GCC 10 happily takes it.)
Re:Show HN: A portable hash map in C by zabzonk on Sunday December 08, 2024 @04:36PM (#42360759)
OK, thank you. But why do the functions have those unused parameters?
Re:Show HN: A portable hash map in C by Sesse__ on Sunday December 08, 2024 @04:39PM (#42360783)
I would assume that is because the hash table takes in a “hash this key” function pointer and a “compare these keys” function pointer, and those must contain the size for variable-length keys. So even if your user-supplied functions know the length, or only care about the first byte, they have to conform to that API, and then you get an unused parameter.
Re:Show HN: A portable hash map in C by zabzonk on Sunday December 08, 2024 @04:42PM (#42360829)
Have functions with different names and different numbers of parameters?
Re:Show HN: A portable hash map in C by Sesse__ on Sunday December 08, 2024 @04:47PM (#42360880)
And then have a union between the two different kinds of pointers, and a tag to tell which one is in use, then test that tag everywhere? Why? What good does it bring for that extra complexity?
Re:Show HN: A portable hash map in C by zabzonk on Sunday December 08, 2024 @04:57PM (#42360985)
Doesn't the name of the function select for the types and use of the parameters? But I haven't written in C for a long time, and this I think could be done more clearly in C++.
Re:Show HN: A portable hash map in C by Sesse__ on Monday December 09, 2024 @12:38PM (#42368301)
You're saying you would write the entire hash table four times, with everything duplicated and no reuse? Again: Why? What do you gain compared to one line to suppress a warning?
Re:Show HN: A portable hash map in C by zabzonk on Monday December 09, 2024 @05:19PM (#42371182)
Clarity? And in C++ at least, I would re-write the functions.
Re:Show HN: A portable hash map in C by detaro on Sunday December 08, 2024 @06:04PM (#42361515)
The point is that you can pass any of the hash functions (or your own compatible) to the datastructure, so it'll obviously call of them with the same set of parameters. And hash functions that need all of them will ignore them.
Re:Show HN: A portable hash map in C by saagarjha on Sunday December 08, 2024 @04:49PM (#42360901)
That would be a bad API design.
Re:Show HN: A portable hash map in C by lang4d on Sunday December 08, 2024 @04:23PM (#42360614)
That would be one way to silence an unused variable warning
Re:Show HN: A portable hash map in C by ryanpetrich on Sunday December 08, 2024 @04:22PM (#42360600)
This is to suppress a compiler warning that the k_sz argument is unused.
Re:Show HN: A portable hash map in C by tester756 on Sunday December 08, 2024 @04:25PM (#42360642)
Re:Show HN: A portable hash map in C by secondcoming on Sunday December 08, 2024 @04:23PM (#42360617)
It silences 'unused variable' warnings.