restrict-qualifier

Is it legal to assign a restricted pointer to another pointer, and use the second pointer to modify the value?

旧街凉风 提交于 2019-11-30 09:05:17
问题 Does the following method respect the "restrict" contract? void fun(int* restrict foo) { int* bar = foo + 32; for (int i = 0; i < 32; ++i) *bar = 0; } My guess is no, but I need some clarification. 回答1: Yes, it sure respects the contract. 6.7.3 Type qualifiers 8 An object that is accessed through a restrict -qualified pointer has a special association with that pointer. This association, defined in 6.7.3.1 below, requires that all accesses to that object use, directly or indirectly, the value

Does `const T *restrict` guarantee the object pointed-to isn’t modified?

本秂侑毒 提交于 2019-11-29 04:01:25
Consider the following code: void doesnt_modify(const int *); int foo(int *n) { *n = 42; doesnt_modify(n); return *n; } where the definition of doesnt_modify isn’t visible for the compiler. Thus, it must assume, that doesnt_modify changes the object n points to and must read *n before the return (the last line cannot be replaced by return 42; ). Assume, doesnt_modify doesn’t modify *n . I thought about the following to allow the optimization: int foo_r(int *n) { *n = 42; { /* New scope is important, I think. */ const int *restrict n_restr = n; doesnt_modify(n_restr); return *n_restr; } } This

Is top-level volatile or restrict significant in a function prototype?

廉价感情. 提交于 2019-11-28 13:53:10
Is there any practical difference between the following prototypes? void f(const int *p); void f(const int *restrict p); void f(const int *volatile p); The section C11 6.7.6.3/15 (final sentence) says that top-level qualifiers are not considered for the purposes of determining type compatibility, i.e. it is permitted for the function definition to have different top-level qualifiers on its parameters than the prototype declaration had. However (unlike C++) it does not say that they are ignored completely. In the case of const this is clearly moot; however in the case of volatile and restrict

Does `const T *restrict` guarantee the object pointed-to isn’t modified?

我们两清 提交于 2019-11-27 18:04:17
问题 Consider the following code: void doesnt_modify(const int *); int foo(int *n) { *n = 42; doesnt_modify(n); return *n; } where the definition of doesnt_modify isn’t visible for the compiler. Thus, it must assume, that doesnt_modify changes the object n points to and must read *n before the return (the last line cannot be replaced by return 42; ). Assume, doesnt_modify doesn’t modify *n . I thought about the following to allow the optimization: int foo_r(int *n) { *n = 42; { /* New scope is

Does restrict help in C if a pointer is already marked const?

依然范特西╮ 提交于 2019-11-27 17:18:11
问题 Just wondering: When I add restrict to a pointer, I tell the compiler that the pointer is not an alias for another pointer. Let's assume I have a function like: // Constructed example void foo (float* result, const float* a, const float* b, const size_t size) { for (size_t i = 0; i < size; ++i) { result [i] = a [0] * b [i]; } } If the compiler has to assume that result might overlap with a , it has to refetch a each time. But, as a is marked const , the compiler could also assume that a is

Realistic usage of the C99 'restrict' keyword?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 09:58:48
I was browsing through some documentation and questions/answers and saw it mentioned. I read a brief description, stating that it would be basically a promise from the programmer that the pointer won't be used to point somewhere else. Can anyone offer some realistic cases where its worth actually using this? Michael restrict says that the pointer is the only thing that accesses the underlying object. It eliminates the potential for pointer aliasing, enabling better optimization by the compiler. For instance, suppose I have a machine with specialized instructions that can multiply vectors of

Is top-level volatile or restrict significant in a function prototype?

為{幸葍}努か 提交于 2019-11-27 08:08:39
问题 Is there any practical difference between the following prototypes? void f(const int *p); void f(const int *restrict p); void f(const int *volatile p); The section C11 6.7.6.3/15 (final sentence) says that top-level qualifiers are not considered for the purposes of determining type compatibility, i.e. it is permitted for the function definition to have different top-level qualifiers on its parameters than the prototype declaration had. However (unlike C++) it does not say that they are

Does the restrict keyword provide significant benefits in gcc/g++?

99封情书 提交于 2019-11-27 06:33:29
Has anyone ever seen any numbers/analysis on whether or not use of the C/C++ restrict keyword in gcc/g++ actual provides any significant performance boost in reality (and not just in theory)? I've read various articles recommending / disparaging its use, but I haven't ran across any real numbers practically demonstrating either sides arguments. EDIT I know that restrict is not officially part of C++, but it is supported by some compilers and I've read a paper by Christer Ericson which strongly recommends its usage. Nils Pipenbrinck The restrict keyword does a difference. I've seen improvements

Does the restrict keyword provide significant benefits in gcc/g++?

霸气de小男生 提交于 2019-11-26 17:35:44
问题 Has anyone ever seen any numbers/analysis on whether or not use of the C/C++ restrict keyword in gcc/g++ actual provides any significant performance boost in reality (and not just in theory)? I've read various articles recommending / disparaging its use, but I haven't ran across any real numbers practically demonstrating either sides arguments. EDIT I know that restrict is not officially part of C++, but it is supported by some compilers and I've read a paper by Christer Ericson which

Realistic usage of the C99 &#39;restrict&#39; keyword?

吃可爱长大的小学妹 提交于 2019-11-26 15:01:33
问题 I was browsing through some documentation and questions/answers and saw it mentioned. I read a brief description, stating that it would be basically a promise from the programmer that the pointer won't be used to point somewhere else. Can anyone offer some realistic cases where its worth actually using this? 回答1: restrict says that the pointer is the only thing that accesses the underlying object. It eliminates the potential for pointer aliasing, enabling better optimization by the compiler.