This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Does malloc tell us anything about alignment modulo k?
- From: Geoffrey Keating <geoffk at geoffk dot org>
- To: Daniel Berlin <dberlin at dberlin dot org>
- Cc: gcc at gcc dot gnu dot org
- Date: 15 Jul 2004 12:30:53 -0700
- Subject: Re: Does malloc tell us anything about alignment modulo k?
- References: <3F979366-D66C-11D8-A090-000A95DA505C@dberlin.org>
Daniel Berlin <dberlin@dberlin.org> writes:
> I'm working on some trivial alignment analysis.
> Malloc'd memory is supposed to be "aligned suitably for any data type".
> Does this actually tell us anything about whether the result is
> aligned modulo k (IE aligned to a k byte boundary)?
>
> Some OS'en seem to have stricter guarantees (Darwin guarantees the
> result of malloc is 16 byte aligned), i'm just trying to figure out
> whether the general case tells us anything.
In general, if malloc(x) is called, and a type exists on the target
whose size is no more than x and which has alignment y, then the
result of the malloc also has alignment at least y.
So, for instance, you can deduce that malloc(16) on Darwin is 16-byte
aligned. You can't deduce the same for malloc(2), but it's not clear
how knowing that's alignment would be useful anyway.