You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When applied to a function declaration or definition that returns a pointer type, `restrict` tells the compiler that the function returns an object that is not *aliased*, that is, referenced by any other pointers. This allows the compiler to perform additional optimizations.
Applied to a function declaration or definition that returns a pointer type and tells the compiler that the function returns an object that will not be aliased with any other pointers.
30
+
## Remarks
31
+
32
+
The compiler propagates `__declspec(restrict)`. For example, the CRT `malloc` function has a `__declspec(restrict)` decoration, and therefore, the compiler assumes that pointers initialized to memory locations by `malloc` are also not aliased by previously existing pointers.
33
+
34
+
The compiler does not check that the returned pointer is not actually aliased. It is the developer's responsibility to ensure the program does not alias a pointer marked with the `restrict __declspec` modifier.
24
35
25
-
## Syntax
26
-
27
-
```
28
-
__declspec(restrict) return_type f();
29
-
```
30
-
31
-
## Remarks
32
-
The compiler will propagate `__declspec(restrict)`. For example, the CRT `malloc` function is decorated with `__declspec(restrict)` and therefore, pointers initialized to memory locations with `malloc` are also implied to not be aliased.
33
-
34
-
The compiler does not check that the pointer is actually not aliased. It is the developer's responsibility to ensure the program does not alias a pointer marked with the `restrict __declspec` modifier.
35
-
36
-
For similar semantics on variables, see [__restrict](../cpp/extension-restrict.md).
36
+
For similar semantics on variables, see [__restrict](../cpp/extension-restrict.md).
37
+
38
+
For another annotation that applies to aliasing within a function, see [__declspec(noalias)](../cpp/noalias.md).
37
39
40
+
For information about the **restrict** keyword that is part of C++ AMP, see [restrict (C++ AMP)](../cpp/restrict-cpp-amp.md).
41
+
38
42
## Example
39
-
See [noalias](../cpp/noalias.md) for an example using `restrict`.
40
-
41
-
For information about the restrict keyword that is part of C++ AMP, see [restrict (C++ AMP)](../cpp/restrict-cpp-amp.md).
42
-
43
-
**END Microsoft Specific**
44
-
45
-
## See Also
46
-
[__declspec](../cpp/declspec.md)
47
-
[Keywords](../cpp/keywords-cpp.md)
43
+
44
+
The following sample demonstrates the use of `__declspec(restrict)`.
45
+
46
+
When `__declspec(restrict)` is applied to a function that returns a pointer, this tells the compiler that the memory pointed to by the return value is not aliased. In this example, the pointers `mempool` and `memptr` are global, so the compiler can't be sure that the memory they refer to is not aliased. However, they are used within `ma` and its caller `init` in a way that returns memory that isn't otherwise referenced by the program, so `__decslpec(restrict)` is used to help the optimizer. This is similar to how the CRT headers decorate allocation functions such as `malloc` by using `__declspec(restrict)` to indicate that they always return memory that cannot be aliased by existing pointers.
0 commit comments