3 ms·
Author here. In the 2 years (almost 3 now) since this was written, I've come around on this argument. I often use fixed size buffers when I know how much space
by ddevault 7y ago
Author here. In the 2 years (almost 3 now) since this was written, I've come around on this argument. I often use fixed size buffers when I know how much space I'll need in advance, of course being careful to measure twice, cut once.
- rlonn 7y agoI use fixed-size buffers as much as possible in C. It's only when I feel a need to conserve memory that I'll use dynamic allocation. For me, I think this pattern helps me avoid bugs by maintaining a healthy (i.e. very high) level of paranoia when dealing with buffers. My first thought when doing something involving a buffer is always "how much space have I allocated here?". With string buffers I'll never use the last byte of the buffer - that one is permanently set to zero and never read or written to. I'll commonly zero the whole buffer before writing to it also. That way any operation not doing proper null-termination will not matter.