This is the mail archive of the
mailing list for the GCC project.
Re: C as used/implemented in practice: analysis of responses
- From: Joseph Myers <joseph at codesourcery dot com>
- To: Peter Sewell <Peter dot Sewell at cl dot cam dot ac dot uk>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Fri, 26 Jun 2015 17:08:26 +0000
- Subject: Re: C as used/implemented in practice: analysis of responses
- Authentication-results: sourceware.org; auth=none
- References: <CAHWkzRQ4+-s6RJeAnCHFDqfQDNaPDATRYkz73xvpwxqP4O9JjA at mail dot gmail dot com>
On Fri, 26 Jun 2015, Peter Sewell wrote:
> **If you calculate an offset between two separately allocated C memory
> objects (e.g. malloc'd regions or global or local variables) by
> pointer subtraction, can you make a usable pointer to the second by
> adding the offset to the address of the first?**
> For GCC, one respondent writes the following, but doesn't give a reason:
> - This is not safe in practice even if the alignment is sufficient
> (and if the alignment of the type is less than its size, obviously
> such a subtraction can't possibly work even with a naive compiler).
It's s simple matter of points-to analysis. &foo + anything may be
assumed (in practice) to point to something within foo (or just past the
end) and not to alias anything accessed through a pointer based on &bar.
If the compiler can see something like &foo + (&bar - &foo) there is no
guarantee of whether it will assume it to point within foo or bar and that
may not be consistent for different uses (so it may end up concluding the
pointer compares unequal to itself).
Joseph S. Myers