N
Nick
Ben Bacarisse said:It's probably worth saying that in this specific instance (where the
buffer elements are characters) one can combine some of the
advantages of both (single allocation/free and some degree of locality)
by doing this:
struct buffer *bp = malloc(sizeof *bp + BUF_LENGTH);
if (bp) {
bp->len = BUF_LENGTH;
bp->buf = (char *)bp + sizeof *bp;
}
Space is wasted and it it of no use if one is trying to match a file
format, but it is well-defined where C99's solution is not available.
Even so, I am not sure I'd bother.
I do that somewhere. One reason is that you can mix-and-match these
structures with others where the pointer points to a string held
elsewhere. Each needs its own allocate and initialise function(s), but
you can then do whatever you like to them, including free-ing.