Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You don't need to check !!indirect. There is no cases that the indirect pointer is NULL:

  while (!!(*indirect) && (*indirect) != entry)
    indirect = &(*indirect)->next;
Also I would rather use * indirect instead of !!(* indirect).


correct. I was just being overly pedantic


Why the !! ? I thought that only makes sense in JS.


I thought that only makes sense in JS.

In C any nonzero value is considered "truthy", and on most architectures NULL is defined to be (void * )(0) or similar. The logical not operator AKA bang operator will replace truthiness with 0, and falsiness with 1. So applying it twice collapses all nonzero values to 1.


Well, yes, but that does absolutely nothing in this code, as it is immediately used as the argument to an if statement.


Indeed, I was only commenting on the JS vs C part.


I'm not sure about the current state of compiler optimisations, but IIRC in the olde-days, a not not in an if() statement would assemble to JZ, saving a clock cycle (& an opcode?), and wouldn't need to stall to load in the full width of the register. Today's branch predicting compilers are beyond me.

I still use it as a clarification that it's a deliberate boolean operation, rather than implicit.


I highly doubt that any modern compiler would generate different code with or without the !!. Not even sure why an old compiler would do that? It's the exact same semantics, surely.


That had nothing to do with pedantry.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: