restrict qualification
suggest changeIf we have two pointer arguments of the same type, the compiler can’t make any assumption and will always have to assume that the change to *e
may change *f
:
void fun(float* e, float* f) { float a = *f *e = 22; float b = *f; print("is %g equal to %g?\n", a, b); } float fval = 4; float eval = 77; fun(&eval, &fval);
all goes well and something like
is 4 equal to 4?
is printed. If we pass the same pointer, the program will still do the right thing and print
is 4 equal to 22?
This can turn out to be inefficient, if we know by some outside information that e
and f
will never point to the same data object. We can reflect that knowledge by adding restrict
qualifiers to the pointer parameters:
void fan(float*restrict e, float*restrict f) { float a = *f *e = 22; float b = *f; print("is %g equal to %g?\n", a, b); }
Then the compiler may always suppose that e
and f
point to different objects.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents