Static Sized Buffers

Allocating memory using statically sized buffers is one of the easiest and most efficient ways to handle using standard C and only standard libraries. Either you allocate permanent memory from data segment (global/static variables) or local memory from stack.

This creates some restrictions though. You have to use "large enough" buffers to make sure the data isn't truncated, but not too large if you want your program's memory usage to stay reasonable. Most people just pick some size which they believe is more than large enough for most uses.

Problems happen when the program is extended to other than the original intended use. The "large enough" buffers suddenly aren't large enough and have to be replaced with even larger buffers, or possibly with dynamic allocations if the new use requires supporting buffers sized in megabytes rather than kilobytes.

Using named buffer sizes can help a lot in some cases. For example instead of using char path[1024] you should use char path[PATH_MAX] to ensure that the program works with systems supporting larger paths.

Static buffers also require you to be careful not to overflow them. You should never assume the buffer is large enough to hold some value, even if it looks that way. This prevents a few problems: