man malloc

Erik Trulsson ertr1013 at student.uu.se
Wed Aug 17 21:32:47 GMT 2005


On Thu, Aug 18, 2005 at 01:03:46AM +0400, 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?

In what way is that difficult to understand?  It can't really be expressed any
simpler, and it means exactly what it says:  That the storage allocated by malloc is
suitably aligned for storing any kind of object.

As an example, it is not uncommon for many systems to require that a
32-bit integer must be aligned on a 4-byte boundary. (I.e. if the CPU tries
to access such an object placed on an address that is not a multiple of 4,
then the program will crash.)  Exactly what alignment is required for
different objects can vary quite a bit, but malloc guarantees that the
storage it allocates is aligned in such a way that you can store any kind
object in it (assuming it is large enough, of course.)



> What is pointer coercion?

No idea.  It is not standard terminology anway.

> I have no pointer before malloc() returns.

Then where do you store the value returned by malloc?
You almost certainly do have some pointer even before malloc returns, but
that pointer might not contain any useful value.


-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013 at student.uu.se


More information about the freebsd-questions mailing list