Comment on Ask HN: Favorite pointer tricks in C?parentComments−bbulkow15yThis avoids allocation and is safe (the cost of two extra compares is low because they'll branch the same way all the time, so you'll get branch prediction power) char *bufp; uint8_t buf[1024]; if (need_sz >= 1024) bufp = malloc(need_sz); else bufp = buf; .... if (bufp != buf) free(bufp);
Comments
This avoids allocation and is safe (the cost of two extra compares is low because they'll branch the same way all the time, so you'll get branch prediction power)