Skip to content

Commit 404fab6

Browse files
openpublishbuildColin Robertson
authored andcommitted
Confirm merge from FromPublicMasterBranch to master to sync with https://github.com/MicrosoftDocs/cpp-docs (branch master) (#596)
* Update cricheditctrl-class.md LineLength is a wrapper of EM_LINELENGTH, which returns length in characters not bytes. * Update csettingsstore-class.md contraction between the meaning of bAdmin in the parameters and remarks section of CSettingsStore::CSettingsStore * Update clistbox-class_23.cpp the buffer required by later code would be 16 bytes in ANSI and 32 bytes in Unicode, preallocating 10 bytes is useless. * Fix unwind opcode values * Update algorithm.md Fix minor typo in Remarks section * Undesired indentation in CFile. Fix for undesired indentation in CFile constructor documentation. * Update cricheditctrl-class.md clarify that length is in TCHARs, add situations when index is out of bound, and when there's a carriage return based on EM_LINELENGTH docs * Update compiler-warning-level-4-c4295.md Clarify warning intent and ways to fix it, per comment by Kinichiro_Inoguchi * Fix type error on the namespace * Merge to live for offline publishing (MicrosoftDocs#532) * Confirm merge from FromPublicMasterBranch to master to sync with https://github.com/MicrosoftDocs/cpp-docs (branch master) (#527) * Update cricheditctrl-class.md LineLength is a wrapper of EM_LINELENGTH, which returns length in characters not bytes. * Update csettingsstore-class.md contraction between the meaning of bAdmin in the parameters and remarks section of CSettingsStore::CSettingsStore * Update clistbox-class_23.cpp the buffer required by later code would be 16 bytes in ANSI and 32 bytes in Unicode, preallocating 10 bytes is useless. * Fix unwind opcode values * Update algorithm.md Fix minor typo in Remarks section * Undesired indentation in CFile. Fix for undesired indentation in CFile constructor documentation. * Update cricheditctrl-class.md clarify that length is in TCHARs, add situations when index is out of bound, and when there's a carriage return based on EM_LINELENGTH docs * Update compiler-warning-level-4-c4295.md Clarify warning intent and ways to fix it, per comment by Kinichiro_Inoguchi * Fix type error on the namespace * Add /Zc:ternary switch docs (#525) * Create topic, not linked yet * Include links in TOC and overview * Fix dash * Add link to Zc:ternary to permissive * Fix link typo * fixed up art files for VC++ directories (#529) * CMake updates for 15.6 (#524) * added new content on cmake cache import * added new art for cmake * updates to cmake tools intro * new art for 15.6 updates * updates for environment variables and inherited environments * proofreading pass * updated art for what's new page (MicrosoftDocs#528) * Second attempt at merge (#531) * added new content on cmake cache import * added new art for cmake * updates to cmake tools intro * new art for 15.6 updates * updates for environment variables and inherited environments * proofreading pass * Update cmake-tools-for-visual-cpp.md (MicrosoftDocs#530) I played editor in this file. I only meant to change one thing, but it was hard to stop. * [mkdir-wmkdir.md] Fix wrong indentation Remove redundant spaces in the code. * Change integer limits and header name to use standard names. * Fix double code quote. * Fix indentation [ _rotl ] * Update get-wpgmptr.md Mention entry point restriction on using _get_wpgmptr() - only for programs for wide entry points. * Update get-pgmptr.md Mention entry point restriction on calling _get_pgmptr * Fix formatting * Remove __declspec(noalias) from example Remove __declspec(noalias) from example to make it clearer what is going on. I'm submitting a second PR that updates __declspec(alias) to have its own example. Clean up other text. * Remove __declspec(restrict) from example Remove __declspec(restrict) from example to make it clearer what is going on. I'm submitting a second PR that updates __declspec(restrict) to have its own example. Clean up other text. * Update date, formatting and some wording. * Update date, additional language cleanup
1 parent 37ad87e commit 404fab6

File tree

1 file changed

+98
-24
lines changed

1 file changed

+98
-24
lines changed

docs/cpp/restrict.md

Lines changed: 98 additions & 24 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,30 +18,104 @@ manager: "ghogen"
1818
ms.workload: ["cplusplus"]
1919
---
2020
# restrict
21-
**Microsoft Specific**
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*();
2229
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.
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.
2435

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).
3739

40+
For information about the **restrict** keyword that is part of C++ AMP, see [restrict (C++ AMP)](../cpp/restrict-cpp-amp.md).
41+
3842
## 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.
47+
48+
```C
49+
// declspec_restrict.c
50+
// Compile with: cl /W4 declspec_restrict.c
51+
#include <stdio.h>
52+
#include <stdlib.h>
53+
54+
#define M 800
55+
#define N 600
56+
#define P 700
57+
58+
float * mempool, * memptr;
59+
60+
__declspec(restrict) float * ma(int size)
61+
{
62+
float * retval;
63+
retval = memptr;
64+
memptr += size;
65+
return retval;
66+
}
67+
68+
__declspec(restrict) float * init(int m, int n)
69+
{
70+
float * a;
71+
int i, j;
72+
int k=1;
73+
74+
a = ma(m * n);
75+
if (!a) exit(1);
76+
for (i=0; i<m; i++)
77+
for (j=0; j<n; j++)
78+
a[i*n+j] = 0.1f/k++;
79+
return a;
80+
}
81+
82+
void multiply(float * a, float * b, float * c)
83+
{
84+
int i, j, k;
85+
86+
for (j=0; j<P; j++)
87+
for (i=0; i<M; i++)
88+
for (k=0; k<N; k++)
89+
c[i * P + j] =
90+
a[i * N + k] *
91+
b[k * P + j];
92+
}
93+
94+
int main()
95+
{
96+
float * a, * b, * c;
97+
98+
mempool = (float *) malloc(sizeof(float) * (M*N + N*P + M*P));
99+
100+
if (!mempool)
101+
{
102+
puts("ERROR: Malloc returned null");
103+
exit(1);
104+
}
105+
106+
memptr = mempool;
107+
a = init(M, N);
108+
b = init(N, P);
109+
c = init(M, P);
110+
111+
multiply(a, b, c);
112+
}
113+
```
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)