- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's the same example as in 6.7.3.1 of C99 spec.
EXAMPLE 3 The function parameter declarations
void h(int n, int * restrict p, int * restrict q, int * restrict r)
{
int i;
for (i = 0; i < n; i++)
p[i] = q[i] + r[i];
}
illustrate how an unmodified object can be aliased through two restricted pointers. In particular, if a and b are disjoint arrays, a call of the form h(100, a, b, b) has defined behavior, because array b is not modified within function h.
In short, it is explicitly mentioned it's defined behavior if b is not modified within function h. However, whether is it undefined behavior if a call of the form h(100, a, a, b)
?
A little bit more backgrounds why I want to make it clear. There are some basic functions which we want to use in in-place or out-of-place manner. In order to reduce the effort, it is desired if we don't need to provide both h(int n, int * restrict p, int * restrict q, int * restrict r)
and h_inplace(int n, int * restrict p, int * restrict q)
. From current observation, it seems icc and msvc can give correct result even if we call it as the form of h(100, a, a, b)
. However, we definitely don't want to have the risk if it is undefined behavior (which means it may be wrong from other compilers or future versions of icc and msvc). What do you think?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The rules about repeated restricted pointers have been discussed more thoroughly in the fortran analogue. When an operand is modified through one of those pointers, reference through the other may pick up the operand either before or after modification. Thus the restrict qualifier could produce undefined behavior.
It's reasonable to expect __restrict and restrict to work the same in compilers which implement both, although I've never seen anyone promising this. They have effect only for modified operands.
I have dealt with customers who insist they have never been bitten by violating the aliasing rules and refuse to take the measures to use the repeated pointers safely.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Tim, could you give me the link which has discussions for fortran?
Thanks,
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page