Here is the code I wrote (using the GCC __restrict__ extension for C ++):
Or C version (in case the C ++ version is not clear due to the use of the extension that every popular C ++ compiler supports) using GCC:
#include <stdio.h> int main(void) { int i = 7; int *restrict a = &i; *a = 5; int *b = &i, *c = &i; *b = 8; *c = 9; printf("%d \n", **&a); // *a - which prints 9 in this case return 0; }
From what I read, if I do *a = 5 , it changes the value of the memory that it points to, a ; after that, the memory that he points to should not be changed by anyone other than a , which means that these programs are incorrect, because b and c change it after that. Or even if b first changes i , after that only a should have access to this memory ( i ). Did I understand correctly?
PS: The restriction in this program does not change anything. With or without restriction, the compiler will produce the same assembly code. I wrote this program to clarify the situation, this is not a good example of using restrict . You can see a good example of using restrict here: http://cellperformance.beyond3d.com/articles/2006/05/demystifying-the-restrict-keyword.html
Lilian A. Moraru
source share