man malloc

Chuck Swiger cswiger at
Wed Aug 17 21:45:58 GMT 2005

Sergey Matveychuk wrote:
> I know it may be stupid, but I can't understand this sentence from 
> malloc(3) man page:
> "
> The allocated space is suitably aligned (after possible pointer 
> coercion) for storage of any type of object.
> "
> What does "suitable aligned for storage of *any* type of object" means?

On some platforms, it is either desirable or required that, say, a 8-byte 
double is stored at a memory location which is is also aligned to 8-bytes:

0x1000 for example, rather than any of (0x1001, 0x1002, 0x1003, ... 0x1007)

> What is pointer coercion? I have no pointer before malloc() returns.

Right.  Well, malloc returns a (void *), but most people want to use the memory 
malloc returns to hold their own arrays, structs, whatever, which means that 
you need to be able to coerce the (void *) malloc gave you into whatever 
pointer type you want to actually use.

So the memory malloc gives you needs to be aligned so that it's OK to be used 
for even the most restrictive datatype known to the system, commonly 8, 16, or 
32 bytes.


More information about the freebsd-questions mailing list