Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 17 Aug 2005 17:45:58 -0400
From:      Chuck Swiger <cswiger@mac.com>
To:        Sergey Matveychuk <sem@FreeBSD.org>
Cc:        questions@FreeBSD.org
Subject:   Re: man malloc
Message-ID:  <4303B016.3030201@mac.com>
In-Reply-To: <4303A632.1000809@FreeBSD.org>
References:  <4303A632.1000809@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
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.

-- 
-Chuck



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4303B016.3030201>