let's look at this given code:
void free(void *ap) {
Header *bp, *p;
bp = (Header *)ap - 1;
for (p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
break;
if (bp + bp->s.size == p->s.ptr) {
bp->s.size += p->s.ptr->s.size;
bp->s.ptr = p->s.ptr->s.ptr;
} else
bp->s.ptr = p->s.ptr;
if (p + p->s.size == bp) {
p->s.size += bp->s.size;
p->s.ptr = bp->s.ptr;
} else
p->s.ptr = bp;
freep = p;
}
As I understand if this given free gets a pointer to already existed memory block, this loop:
for (p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
break;
will actually goes forever. Because when p==ap and for every other p the condition:!(bp > p && bp < p->s.ptr) will be true and the condition p >= p->s.ptr && (bp > p || bp < p->s.ptr) will be false.
Editing: The full code can be seen here: Explain this implementation of malloc from the K&R book