This is the mail archive of the gcc@gcc.gnu.org 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: Restrict implementation considered harmful


Richard Guenther <rguenther@suse.de> writes:

> int foo (int *__restrict p)
> {
>   int *__restrict q;
>   int v;
>   q = p + 1;
>   q = q - 1;
>   v = *q;
>   *p = 1;
>   return v + *q;
> }
> extern void abort (void);
> int main()
> {
>   int i = 0;
>   if (foo (&i) != 1)
>     abort ();
>   return 0;
> }

This program appears to me to be invalid according to C99 6.7.3.1,
which is the only definition of restrict that we have.

    If P is assigned the value of a pointer expression E that is based
    on another restricted pointer object P2, associated with block B2,
    then either the execution of B2 shall begin before the execution
    of B, or the execution of B2 shall end prior to the assignment.
    If these requirements are not met, then the behavior is undefined.

In your program, P is q and P2 is p.  Block B and B2 are the same
block: the only block in foo.  It is obviously not the case that the
block executes before itself.  So I believe the assignment "q = p + 1"
is undefined behaviour.

More generally, as I understand the restrict qualifier, it means that
if two pointers both have the restrict qualifier, and you use one of
the pointers to modify an object, you may not use the other pointer to
access that object.  Your program clearly violates that guideline.

Ian


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