Skip to content

Commit 02c6d1a

Browse files
author
Colin Robertson
authored
Update date, additional language cleanup
1 parent 6537334 commit 02c6d1a

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

docs/cpp/restrict.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "restrict | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "11/04/2016"
4+
ms.date: "02/09/2018"
55
ms.reviewer: ""
66
ms.suite: ""
77
ms.technology: ["cpp-language"]
@@ -18,37 +18,36 @@ manager: "ghogen"
1818
ms.workload: ["cplusplus"]
1919
---
2020
# restrict
21-
**Microsoft Specific**
22-
23-
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.
24-
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 by previously existing memory.
21+
22+
**Microsoft Specific**
23+
24+
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.
25+
26+
## Syntax
27+
28+
> **__declspec(restrict)** *pointer_return_type* *function*();
3329
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.
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.
3535

36-
For similar semantics on variables, see [__restrict](../cpp/extension-restrict.md).
36+
For similar semantics on variables, see [__restrict](../cpp/extension-restrict.md).
3737

38-
For another annotation that can impact aliasing, see also [__declspec(noalias)](../cpp/noalias.md).
38+
For another annotation that applies to aliasing within a function, see [__declspec(noalias)](../cpp/noalias.md).
3939

40-
For information about the restrict keyword that is part of C++ AMP, see [restrict (C++ AMP)](../cpp/restrict-cpp-amp.md).
40+
For information about the **restrict** keyword that is part of C++ AMP, see [restrict (C++ AMP)](../cpp/restrict-cpp-amp.md).
4141

4242
## Example
4343

44-
The following sample demonstrates using `__declspec(restrict)`.
45-
46-
Decorating functions that return pointers with `__declspec(restrict)` 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 has no assurance that the memory is not subject to aliasing. However, they are used in such a way that each invocation of `ma` and `init` return 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 `malloc` as `restrict` to indicate that they always return memory that cannot be aliased by existing pointers.
44+
The following sample demonstrates the use of `__declspec(restrict)`.
4745

48-
Decorating functions with `__declspec(noalias)` tells the compiler that the function does not interfere with the global state except through the pointers in its parameter list. in the example that accesses memory
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.
4947

5048
```C
51-
// declspec_noalias.c
49+
// declspec_restrict.c
50+
// Compile with: cl /W4 declspec_restrict.c
5251
#include <stdio.h>
5352
#include <stdlib.h>
5453

@@ -76,7 +75,7 @@ __declspec(restrict) float * init(int m, int n)
7675
if (!a) exit(1);
7776
for (i=0; i<m; i++)
7877
for (j=0; j<n; j++)
79-
a[i*n+j] = 0.1/k++;
78+
a[i*n+j] = 0.1f/k++;
8079
return a;
8180
}
8281

@@ -112,11 +111,11 @@ int main()
112111
multiply(a, b, c);
113112
}
114113
```
115-
116-
117-
**END Microsoft Specific**
118-
119-
## See Also
120-
[__declspec](../cpp/declspec.md)
121-
[Keywords](../cpp/keywords-cpp.md)
122-
[__declspec(noalias)](../cpp/noalias.md)
114+
115+
**END Microsoft Specific**
116+
117+
## See also
118+
119+
[Keywords](../cpp/keywords-cpp.md)
120+
[__declspec](../cpp/declspec.md)
121+
[__declspec(noalias)](../cpp/noalias.md)

0 commit comments

Comments
 (0)