3 ms·
In the case where you expire the last entry of the linked list you will SEGFAULT. A test for head being NULL at the end of the loop would take care of that, but
by jws 16y ago
In the case where you expire the last entry of the linked list you will SEGFAULT. A test for head being NULL at the end of the loop would take care of that, but I'd put it in the condition of the while loop since it is probably free on a modern processor given the memory access pattern and it makes something sane happen when a client calls find(NULL,...).
I also don't think the last element ages, but since aging is unrelated to reality that is ok. On second thought, it protects you from the SEGFAULT because the last element can never expire. Very cunning of you.
- fragmede 16y agoI'm not seeing the segfault, but it does leak memory like a sieve with no filter. Good point re: age. ++head->age should be the first operation of the while loop.
- jws 16y agoJust in case someone is scratching their head over this... Let's come into the while loop body with data like this where H is the head pointer, C and E are elements of the list... H -> C /* head points to current node */ C->next -> E /* expired node is next*/ E->next -> NULL /* expired node is last */ if ( head->next == NULL)... /* no, head points to C, C->next is E */ if ( head->next->age > EXP...) /* yes, head->next is E and is expired */ head->next = head->next->next /* C->next is now NULL (E->next) */ head = head->next; /* head is now NULL */ /* back to the top of the loop */ while( *head != NULL... /* BANG! a NULL value just got dereferenced */