Live data from Hacker News

15-line hash table in C

pastes.archbsd.net

101–104 of 104 posts

Re: 15-line hash table in C

#102
post #100

Earlier quoted context omitted.

Note that on many systems (e.g. Linux), malloc/calloc won't always return NULL when you're out of memory because of lazy memory allocation policies. It will only crash when you start reading / writing. That makes it arguably less useful to test the return value. edit: clarity.

However, mmap and the functions which rely on it will return MAP_FAILED/NULL if no gap large enough is found in the virtual address space or if you've used MAP_FIXED and the area isn't free. If you don't check for errors, the application could potentially end up trying to dereference a NULL pointer.

Ok, let me rephrase then: On modern systems, checking the return value of malloc() is not a proper way to check that the system is out of memory and therefore it doesn't guarantee that the program will run correctly in any case.

That's what I wanted to point, and I agree that it remains useful to detect other types of errors such as the ones you mentioned.

Re: 15-line hash table in C

#103
A bug nobody has pointed out yet is that star-star-t (sorry, can't figure out this site's markup for escaping stars) dereferences a null pointer. It appears to work because gcc decides to generate the same code for star-star-t as for star-t (which is correct behaviour if star-t is not NULL, and moot if t is NULL). The code should just be star-t there.

Also, sizeof(int star-star) in hnew should be sizeof( int (star)[2] ).

Post reply on HN