dynamic buffer allocation at char buf[1]

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.
 
N

Nick

Willem said:
Eric Sosman wrote:
) On 4/11/2010 9:34 AM, Willem wrote:
)> - A lot of file formats consist of blocks with a fixed-size header,
)> followed by a variable-size bit of data
)
) For that last, a struct of any kind is risky. Padding,
) you know. (Also byte order and other such representational
) issues, but they're not unique to structs.)

I know. That's why a lot of compilers have extensions to force a
struct to not be padded. Should have been added to the language IMO.

I'd also like the opposite - a way to say "nothing depends on the order
and relative alignment of the members of this structure - feel free to
re-arrange to minimise space, maximise access speed, whatever).

That would be really useful for something like:

struct thing {
char type_of_item_1;
void *item_1;
char type_of_item_2;
void *item_2;
};

or even things involving bit-fields. You can happily put the elements
in the most logical order, without caring that you are wasting memory.
 
P

Phil Carmody

Nick said:
I'd also like the opposite - a way to say "nothing depends on the order
and relative alignment of the members of this structure - feel free to
re-arrange to minimise space, maximise access speed, whatever).

That would be really useful for something like:

struct thing {
char type_of_item_1;
void *item_1;
char type_of_item_2;
void *item_2;
};

or even things involving bit-fields. You can happily put the elements
in the most logical order, without caring that you are wasting memory.

I sometimes wish C compilers could be persuaded to lay out

struct { int x, y, z; } things[1024];

as if it were more like:

struct lots_of_things {
char things_x[1024];
char things_y[1024];
char things_z[1024];
} rearranged_things.

If I'm having to pay attention to cache usage, and I don't use half
of the members of thousands of structure, I don't want to waste the
cache by pulling them in.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top