3 ms·
I 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 patter
by rlonn 7y ago
I 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.