[GRASS-dev] char[1024]

Glynn Clements glynn at gclements.plus.com
Tue Nov 21 15:40:05 EST 2006


Hamish wrote:

> may string variables allocate memory in powers of 2, e.g. char[128] or
> char[1024]. Lots of places in GRASS you see char[100] or char[1000] for
> buffers. Is there a big advantage to fitting exactly in 1024 bytes for
> something like a G_getl() string input buffer?

If there's any "ideal" size for malloc()'d buffers, it's probably a
number slightly smaller than a power of 2, to allow for malloc()
overhead (GNU malloc puts a header at the beginning of each block;
IIRC, BSD malloc keeps separate descriptor blocks).

> I remember Glynn recommending the RGBA_Color struct fit into this sort
> of 2^space but I didn't pick up why this was important.

The issues are different for very small structures, which will
typically get rounded up to a multiple of the system's word size (e.g. 
a 4-byte structure takes 4 bytes but a 5-byte structure takes 8 bytes)
even when they're on the stack.

> ie for a heavily re-used buffer is it a good idea (faster) to use 1024
> instead of 1000? is it only important if you are passing it to another fn?
> speed of alloc() and free()?

In general, I don't think that it really matters unless you are
allocating large numbers of such buffers.

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list