This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C as used/implemented in practice: analysis of responses

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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]