3 ms·
Few people actually know Fortran anymore, but in reality, C99's restrict covers only a narrow subset of what Fortran's aliasing rules covered. restrict's numero
by cliffbean 13y ago
Few people actually know Fortran anymore, but in reality, C99's restrict covers only a narrow subset of what Fortran's aliasing rules covered. restrict's numerous limitations substantially reduce its usefulness, which is one reason it's not as common knowledge as one might guess.
- sharpneli 13y agoCan you elaborate on what the limitations are? I have been under the impression that restrict makes it pretty much equivalent to fortran (restrict means that nothing aliases the pointer)
- cliffbean 13y agoOn some pointers, for example function return types, restrict is defined to be meaningless. Also, restrict pointers can be copies of other pointers, and copies can be made of restrict pointers, subject to certain rules. So it's more involved than just "nothing aliases the pointer". Here's another example: // C has pretty paltry array support, so it's common for // people to build their own stuff like this: struct Array { float *data; size_t len; }; // But then you use it: void foo(Array *a, Array *b) { for (i = ...) for (j = ...) a->data[i] = b->data[j]; } and there's nowhere you can put restrict to declare that the two array references here are independent. The data pointer is one of the contexts where restrict is defined to be meaningless. If you're a hyperactive squirrel, you might try rewriting all your code everywhere to do stuff like this: void foo(Array *a, Array *b) { for (i = ...) for (j = ...) { float *restrict x = a->data; float *restrict y = b->data; x[i] = y[i]; } but then you'll just discover that most compilers ignore restrict in block scopes like this, because the semantics for that are tricky for compilers to deal with, so you still won't win.