Martin said:
I just wanna ask if I have a decent understanding of how restrict
pointers work. Here's an example of a function:
void Func(int *p) /* const I'd usually add has been removed for
simplicity */
{
*p += 7;
*p += 7;
}
Assuming this function is outline (i.e. as opposed to inline), then it
CANNOT be optimised to "+= 14" because the function is not free to
assume that what 'p' points to doesn't get changed in the meantime.
That right?
And IF it were written as: void Func(int *restrict p), then it WOULD
be able to perform such an optimisation? That right?
That's not what restrict is all about.
Take a look at the C99 prototypes for memcpy and memmove:
void * memcpy(void * restrict s1, const void * restrict s2, size_t n);
void *memmove(void * s1, const void * s2, size_t n);
"restrict" means that all accesses to the object pointed to by s1,
will be made by pointers which were derived from s1.
In this version of memcpy:
void *memcpy(void *restrict s1, const void *restrict s2, size_t n)
{
unsigned char *p1 = s1;
const unsigned char *p2 = s2;
while (n-- != 0) {
*p1++ = *p2++;
}
return s1;
}
all accesses to the object pointed to by s1,
are made through various values of p1, but p1 is derived from s1.
In memmove, because the objects may overlap,
It's possible that s1 and/or pointers derived from s1,
may point to the same addresses
as pointed to by s2 and/or pointers derived from s2.