restrict-qualifier

What can human beings make out of the restrict qualifier?

流过昼夜 提交于 2019-12-20 10:29:15
问题 If I got the C99 restrict keyword right, qualifying a pointer with it is a promise made that the data it references won't be modified behind the compiler's back through aliasing. By contrast, the way I understand the const qualifier is as compiler-enforced documentation that a given object won't be modified behind the back of a human being writing code. The compiler might get a hint as a side effect, but as a programmer I don't really care. In a similar way, would it be appropriate to

Restricted pointer questions

江枫思渺然 提交于 2019-12-19 08:10:41
问题 I'm a little confused about the rules regarding restricted pointers. Maybe someone out there can help me out. Is it legal to define nested restricted pointers as follows: int* restrict a; int* restrict b; a = malloc(sizeof(int)); // b = a; <-- assignment here is illegal, needs to happen in child block // *b = rand(); while(1) { b = a; // Is this legal? Assuming 'b' is not modified outside the while() block *b = rand(); } Is it legal to derive a restricted pointer value as follows: int*

Understanding restrict qualifier by examples

。_饼干妹妹 提交于 2019-12-18 14:49:12
问题 The restrict keyword's behavior is defined in C99 by 6.7.3.1: Let D be a declaration of an ordinary identifier that provides a means of designating an object P as a restrict-qualified pointer to type T. If D appears inside a block and does not have storage class extern, let B denote the block. If D appears in the list of parameter declarations of a function definition, let B denote the associated block. Otherwise, let B denote the block of main (or the block of whatever function is called at

Can you use restrict-ed pointers to access the same object in some cases?

浪尽此生 提交于 2019-12-18 08:09:40
问题 Most definitions of restrict say that it's a promise from the programmer to the compiler that for the lifetime of the pointer, the pointer is the only way that object is accessed. This allows the compiler to optimize the output, because it knows that it will be accessed only by one pointer and thus can be changed only by it. Which if I understand correctly usually means that the program doesn't have to reload the value the pointer points to. If this is correct then there should be some

Why does the restrict qualifier still allow memcpy to access overlapping memory?

倖福魔咒の 提交于 2019-12-13 17:28:49
问题 I wanted to see if restrict would prevent memcpy from accessing overlapping memory. The memcpy function copies n bytes from memory area src to memory area dest directly . The memory areas should not overlap. memmove uses a buffer so there is no risk of overlapping memory. The restrict qualifier says that for the lifetime of the pointer, only the pointer itself or a value directly from it (such as pointer + n ) will have access to the data of that object. If the declaration of intent is not

C restrict with typedef

淺唱寂寞╮ 提交于 2019-12-13 02:05:22
问题 i'm doing some code now and got some problem using restrict keyword. typedef int* pt; int foo(pt a, pt b) { ... /* stuff */ } What if I want to make a and b restricted? The code below failed: typedef int* pt; int foo(pt restrict a, pt restrict b) { ... /* stuff */ } Thanks in advance. 回答1: You need a "restricted pointer to integer" int * restrict p not a "pointer to restricted integer" restrict int *p so you will need to make another typedef. You can't "reach inside" the original one. EDIT:

Compatible types and argument type qualifiers

白昼怎懂夜的黑 提交于 2019-12-12 11:26:57
问题 Are the types of these two declarations compatible types? void f(char *, char *); void f(char *restrict, char *restrict); or similarly: void g(char *); void g(char *const); I'm having a hard time finding anything in the standard which covers the issue. I'm mostly interested in the topic of whether it's valid to manually prototype a function, omitting the restrict keyword, where the actual type might have restrict-qualified arguments depending on the version of C or version of other libraries

C++: Bypassing strict-aliasing through union, then use __restrict extension

流过昼夜 提交于 2019-12-11 22:05:20
问题 I wonder if it is possible to tailor strict aliasing requirements to specifically designed cases, while still preserving strict aliasing in general or -O2/-O3 optimization respectively. To be more precise, in cases where it is needed, strict aliasing can be bypassed using an anonymous union (as pointed out here and here): #define PTR_CAST(type, x) &(((union {typeof(*x) src; type dst;}*) &(x))->dst) Now I wonder if using __restrict on a pointer obtained by such a cast would re-enable no-alias

Get rid of “type qualifier” warnings on functions using the restrict keyword

六眼飞鱼酱① 提交于 2019-12-11 08:19:37
问题 I'm trying to clean up warnings that I'm getting when compiling Blitz++ of the form: /opt/local/include/blitz/tinyvec2.h:261:35: warning: type qualifiers ignored on function return type [-Wignored-qualifiers] /opt/local/include/blitz/tinyvec2.h:264:43: warning: type qualifiers ignored on function return type [-Wignored-qualifiers] /opt/local/include/blitz/tinyvec2.h:267:40: warning: type qualifiers ignored on function return type [-Wignored-qualifiers] <<etc.>> From these kinds of member

Passing restrict qualified pointers to functions?

人盡茶涼 提交于 2019-12-10 18:24:59
问题 Restrict qualified pointers were explained to me as having a rule: Any object accessed by the pointer and modified anywhere is only ever accessed by the pointer. So the following does not work, right? void agSum( int * restrict x, int n ){ for(int i=0; i<n-1; i++) x[i+1] += x[i]; } int SumAndFree( int * restrict y, int n ){ agSum(y); printf("%i",y[n-1]); free(y); } So, I guess this is invalid because y[n-1] is modified somewhere not directly accessed from the restrict pointer y, and it is