Skip to content

Commit 10bd1b9

Browse files
TylerMSFTTylerMSFT
authored andcommitted
start restrict work
1 parent 4a12c92 commit 10bd1b9

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

docs/c-language/type-qualifiers.md

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
---
22
title: "Type Qualifiers"
3-
ms.date: "11/04/2016"
3+
ms.date: "10/30/2020"
44
helpviewer_keywords: ["volatile keyword [C], type qualifier", "type qualifiers", "volatile keyword [C]", "qualifiers for types", "const keyword [C]", "memory, access using volatile", "volatile keyword [C], type specifier"]
55
ms.assetid: bb4c6744-1dd7-40a8-b4eb-f5585be30908
66
---
77
# Type Qualifiers
88

99
Type qualifiers give one of two properties to an identifier. The **`const`** type qualifier declares an object to be nonmodifiable. The **`volatile`** type qualifier declares an item whose value can legitimately be changed by something beyond the control of the program in which it appears, such as a concurrently executing thread.
1010

11-
The two type qualifiers, **`const`** and **`volatile`**, can appear only once in a declaration. Type qualifiers can appear with any type specifier; however, they cannot appear after the first comma in a multiple item declaration. For example, the following declarations are legal:
11+
The type qualifiers, **`const`**, **restrict**, and **`volatile`**, can appear only once in a declaration. Type qualifiers can appear with any type specifier; however, they cannot appear after the first comma in a multiple item declaration. For example, the following declarations are legal:
1212

13-
```
13+
```c
1414
typedef volatile int VI;
1515
const int ci;
1616
```
1717

1818
These declarations are not legal:
1919

20-
```
20+
```c
2121
typedef int *i, volatile *vi;
2222
float f, const cf;
2323
```
@@ -26,31 +26,67 @@ Type qualifiers are relevant only when accessing identifiers as l-values in expr
2626

2727
## Syntax
2828

29-
*type-qualifier*:
30-
**constvolatile**
29+
*type-qualifier*: **const** \| **volatile** \| **restrict**
30+
31+
## const and volatile
3132

3233
The following are legal **`const`** and **`volatile`** declarations:
3334

34-
```
35-
int const *p_ci; /* Pointer to constant int */
36-
int const (*p_ci); /* Pointer to constant int */
37-
int *const cp_i; /* Constant pointer to int */
38-
int (*const cp_i); /* Constant pointer to int */
39-
int volatile vint; /* Volatile integer */
35+
```c
36+
int const *p_ci; // Pointer to constant int
37+
int const (*p_ci); /* Pointer to constant int */
38+
int *const cp_i; /* Constant pointer to int */
39+
int (*const cp_i); /* Constant pointer to int */
40+
int volatile vint; /* Volatile integer */
4041
```
4142
4243
If the specification of an array type includes type qualifiers, the element is qualified, not the array type. If the specification of the function type includes qualifiers, the behavior is undefined. Neither **`volatile`** nor **`const`** affects the range of values or arithmetic properties of the object.
4344
44-
This list describes how to use **`const`** and **`volatile`**.
45-
4645
- The **`const`** keyword can be used to modify any fundamental or aggregate type, or a pointer to an object of any type, or a **`typedef`**. If an item is declared with only the **`const`** type qualifier, its type is taken to be **const int**. A **`const`** variable can be initialized or can be placed in a read-only region of storage. The **`const`** keyword is useful for declaring pointers to **`const`** since this requires the function not to change the pointer in any way.
4746
4847
- The compiler assumes that, at any point in the program, a **`volatile`** variable can be accessed by an unknown process that uses or modifies its value. Therefore, regardless of the optimizations specified on the command line, the code for each assignment to or reference of a **`volatile`** variable must be generated even if it appears to have no effect.
4948
5049
If **`volatile`** is used alone, **`int`** is assumed. The **`volatile`** type specifier can be used to provide reliable access to special memory locations. Use **`volatile`** with data objects that may be accessed or altered by signal handlers, by concurrently executing programs, or by special hardware such as memory-mapped I/O control registers. You can declare a variable as **`volatile`** for its lifetime, or you can cast a single reference to be **`volatile`**.
5150
5251
- An item can be both **`const`** and **`volatile`**, in which case the item could not be legitimately modified by its own program, but could be modified by some asynchronous process.
52+
53+
## `restrict`
54+
55+
The **`restrict`** type qualifier, introduced in C99, is used with pointer declarations. It qualifies the pointer, not the object being pointed to. It is a hint to the compiler that for the lifetime of the pointer, only the pointer itself or a value derived from it (such as pointer + 1) will be used to access the object it points at. This means that any loads and stores through the restricted pointer are the only loads and stores to that address for the lifetime of the pointer. This helps the compiler optimize the performance of code that uses the pointer.
56+
57+
If you try to alias the pointer, you will get warning: 4245 For example:
58+
59+
```c
60+
// compile with: /std:c11
61+
62+
void stored(data* restrict dst, const data* restrict src)
63+
{
64+
// Do not trigger warning 4245
65+
dst->i = NO_WARN(-(src->i));
66+
67+
}
68+
69+
void storei(int* restrict dst, const int* restrict src)
70+
{
71+
*dst = *src;
72+
}
73+
74+
int main() {
75+
data src, dst;
76+
src.i = 5;
77+
78+
int i, j;
79+
80+
i = 10;
81+
82+
store(&src, &dst);
83+
store(&i, &j);
84+
85+
my_exit(0);
86+
}
87+
```
5388

5489
## See also
5590

91+
[/std (Specify Language Standard Version)](../build/reference/std-specify-language-standard-version.md)\
5692
[Declarations and Types](../c-language/declarations-and-types.md)

0 commit comments

Comments
 (0)