Skip to content

Commit ec3cc8c

Browse files
author
Colin Robertson
authored
Merge pull request MicrosoftDocs#2586 from MicrosoftDocs/master637405443297845198
Repo sync for protected CLA branch
2 parents 888d3ed + 07180e7 commit ec3cc8c

File tree

5 files changed

+94
-54
lines changed

5 files changed

+94
-54
lines changed

docs/c-language/type-qualifiers.md

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
---
22
title: "Type Qualifiers"
3-
ms.date: "11/04/2016"
3+
description: "Describes type qualifiers for the C language used in the Microsoft Visual C compiler"
4+
ms.date: "11/6/2020"
45
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"]
56
ms.assetid: bb4c6744-1dd7-40a8-b4eb-f5585be30908
67
---
78
# Type Qualifiers
89

910
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.
1011

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:
12+
The type qualifiers, **`const`**, **`restrict`**, and **`volatile`**, can appear only once in a declaration. Type qualifiers can appear with any type specifier; however, they can't appear after the first comma in a multiple item declaration. For example, the following declarations are legal:
1213

13-
```
14+
```c
1415
typedef volatile int VI;
1516
const int ci;
1617
```
1718

18-
These declarations are not legal:
19+
These declarations aren't legal:
1920

20-
```
21+
```c
2122
typedef int *i, volatile *vi;
2223
float f, const cf;
2324
```
@@ -26,31 +27,69 @@ Type qualifiers are relevant only when accessing identifiers as l-values in expr
2627

2728
## Syntax
2829

29-
*type-qualifier*:
30-
**constvolatile**
30+
*`type-qualifier`*:\
31+
 **`const`**\
32+
 **`restrict`**\
33+
 **`volatile`**
34+
35+
## `const` and `volatile`
3136

3237
The following are legal **`const`** and **`volatile`** declarations:
3338

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 */
39+
```c
40+
int const *p_ci; // Pointer to constant int
41+
int const (*p_ci); // Pointer to constant int
42+
int *const cp_i; // Constant pointer to int
43+
int (*const cp_i); // Constant pointer to int
44+
int volatile vint; // Volatile integer
4045
```
4146
42-
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.
43-
44-
This list describes how to use **`const`** and **`volatile`**.
47+
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. **`volatile`** and **`const`** don't affect the range of values or arithmetic properties of the object.
4548
4649
- 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.
4750
48-
- 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.
51+
- 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. 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.
52+
53+
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`**.
54+
55+
- An item can be both **`const`** and **`volatile`**, in which case the item couldn't be legitimately modified by its own program, but could be modified by some asynchronous process.
56+
57+
## `restrict`
4958
50-
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`**.
59+
The **`restrict`** type qualifier, introduced in C99, can be applied to pointer declarations. It qualifies the pointer, not what it points at.
5160
52-
- 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.
61+
**`restrict`** is an optimization hint to the compiler that no other pointer in the current scope refers to the same memory location. That is, only the pointer or a value derived from it (such as pointer + 1) is used to access the object during the lifetime of the pointer. This helps the compiler produce more optimized code. C++ has an equivalent mechanism, [`__restrict`](../cpp/extension-restrict.md)
62+
63+
Keep in mind that **`restrict`** is a contract between you and the compiler. If you do alias a pointer marked with **`restrict`**, the result is undefined.
64+
65+
Here's an example that uses **`restrict`**:
66+
67+
```c
68+
void test(int* restrict first, int* restrict second, int* val)
69+
{
70+
*first += *val;
71+
*second += *val;
72+
}
73+
74+
int main()
75+
{
76+
int i = 1, j = 2, k = 3;
77+
test(&i, &j, &k);
78+
79+
return 0;
80+
}
81+
82+
// Marking union members restrict tells the compiler that
83+
// only z.x or z.y will be accessed in any scope, which allows
84+
// the compiler to optimize access to the members.
85+
union z
86+
{
87+
int* restrict x;
88+
double* restrict y;
89+
};
90+
```
5391

5492
## See also
5593

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

docs/cpp/arrays-cpp.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: "Arrays (C++)"
3-
ms.date: 08/03/2020
3+
description: "Learn how to declare and use the native array type in the standard C++ programming language."
4+
ms.date: 11/08/2020
45
helpviewer_keywords: ["declaring arrays [C++], about declaring arrays", "multidimensional arrays [C++]", "arrays [C++]"]
56
ms.assetid: 3f5986aa-485c-4ba4-9502-67e2ef924238
67
---
@@ -127,7 +128,7 @@ When an array is passed to a function, it's passed as a pointer to the first ele
127128
The following example shows a function that accepts an array and a length. The pointer points to the original array, not a copy. Because the parameter isn't **`const`**, the function can modify the array elements.
128129
129130
```cpp
130-
void process(double p*, const size_t len)
131+
void process(double *p, const size_t len)
131132
{
132133
std::cout << "process:\n";
133134
for (size_t i = 0; i < len; ++i)
@@ -137,10 +138,10 @@ void process(double p*, const size_t len)
137138
}
138139
```
139140

140-
Declare the array as const to make it read-only within the function block:
141+
Declare and define the array parameter `p` as **`const`** to make it read-only within the function block:
141142

142143
```cpp
143-
void process(const double p*, const size_t len);
144+
void process(const double *p, const size_t len);
144145
```
145146
146147
The same function can also be declared in these ways, with no change in behavior. The array is still passed as a pointer to the first element:

docs/cpp/extension-restrict.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
---
2-
title: "__restrict"
3-
ms.date: "10/10/2018"
2+
title: "`__restrict`"
3+
description: "Describes the Microsoft Visual C++ `__restrict` keyword."
4+
ms.date: "11/6/2020"
45
f1_keywords: ["__restrict_cpp", "__restrict", "_restrict"]
56
helpviewer_keywords: ["__restrict keyword [C++]"]
6-
ms.assetid: 2d151b4d-f930-49df-bd16-d8757ec7fa83
77
---
8-
# __restrict
8+
# `__restrict`
99

10-
Like the **__declspec ( [restrict](../cpp/restrict.md) )** modifier, the **`__restrict`** keyword indicates that a symbol is not aliased in the current scope. The **`__restrict`** keyword differs from the `__declspec ( restrict )` modifier in the following ways:
10+
Like the **`__declspec` ( [`restrict`](../cpp/restrict.md) )** modifier, the **`__restrict`** keyword (two leading underscores '_') indicates that a symbol isn't aliased in the current scope. The **`__restrict`** keyword differs from the `__declspec (restrict)` modifier in the following ways:
1111

12-
- The **`__restrict`** keyword is valid only on variables, and `__declspec ( restrict )` is only valid on function declarations and definitions.
12+
- The **`__restrict`** keyword is valid only on variables, and `__declspec (restrict)` is only valid on function declarations and definitions.
1313

14-
- **`__restrict`** is similar to **`restrict`** from the C99 spec, but **`__restrict`** can be used in C++ or C programs.
14+
- **`__restrict`** is similar to [`restrict`](../c-language/type-qualifiers.md#restrict) for C starting in C99, but **`__restrict`** can be used in both C++ and C programs.
1515

16-
- When **`__restrict`** is used, the compiler will not propagate the no-alias property of a variable. That is, if you assign a **`__restrict`** variable to a non-**`__restrict`** variable, the compiler will still allow the non-__restrict variable to be aliased. This is different from the behavior of the **`restrict`** keyword from the C99 specification.
16+
- When **`__restrict`** is used, the compiler won't propagate the no-alias property of a variable. That is, if you assign a **`__restrict`** variable to a non-**`__restrict`** variable, the compiler will still allow the non-__restrict variable to be aliased. This is different from the behavior of the C99 C language **`restrict`** keyword.
1717

18-
Generally, if you affect the behavior of an entire function, it is better to use `__declspec ( restrict )` than the keyword.
18+
Generally, if you want to affect the behavior of an entire function, use **`__declspec (restrict)`** instead of the keyword.
1919

20-
For compatibility with previous versions, **_restrict** is a synonym for **`__restrict`** unless compiler option [/Za \(Disable language extensions)](../build/reference/za-ze-disable-language-extensions.md) is specified.
20+
For compatibility with previous versions, **`_restrict`** is a synonym for **`__restrict`** unless compiler option [`/Za` \(Disable language extensions)](../build/reference/za-ze-disable-language-extensions.md) is specified.
2121

2222
In Visual Studio 2015 and later, **`__restrict`** can be used on C++ references.
2323

2424
> [!NOTE]
25-
> When used on a variable that also has the [volatile](../cpp/volatile-cpp.md) keyword, **`volatile`** will take precedence.
25+
> When used on a variable that also has the [`volatile`](../cpp/volatile-cpp.md) keyword, **`volatile`** will take precedence.
2626
2727
## Example
2828

docs/preprocessor/pragma-directives-and-the-pragma-keyword.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ return _hr; \
134134
The string literal should be what you would otherwise put following a *`#pragma`* statement. For example:
135135
136136
```c
137-
#pragma message("--the #pragma way")
137+
#pragma message("the #pragma way")
138138
_Pragma ("message( \"the _Pragma way\")")
139139
```
140140

docs/standard-library/ios-typedefs.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
---
2-
title: "&lt;ios&gt; typedefs"
3-
ms.date: "11/04/2016"
2+
title: "`<ios>` typedefs"
3+
description: "Describes the C++ standard template library (STL) `<ios>` typedefs that support the `ios` class from the old `iostream` library."
4+
ms.date: "11/06/2020"
45
f1_keywords: ["iosfwd/std::ios", "iosfwd/std::streamoff", "iosfwd/std::streampos", "iosfwd/std::streamsize", "iosfwd/std::wios", "iosfwd/std::wstreampos"]
5-
ms.assetid: 0b962632-3439-44de-bf26-20c67a7f0ff3
66
---
7-
# &lt;ios&gt; typedefs
7+
# `<ios>` typedefs
88

9-
## <a name="ios"></a> ios
9+
## `ios`
1010

11-
Supports the ios class from the old iostream library.
11+
Supports the `ios` class from the old `iostream` library.
1212

1313
```cpp
1414
typedef basic_ios<char, char_traits<char>> ios;
1515
```
1616

1717
### Remarks
1818

19-
The type is a synonym for class template [basic_ios](../standard-library/basic-ios-class.md), specialized for elements of type **`char`** with default character traits.
19+
The type is a synonym for class template [`basic_ios`](../standard-library/basic-ios-class.md), specialized for elements of type **`char`** with default character traits.
2020

21-
## <a name="streamoff"></a> streamoff
21+
## `streamoff`
2222

2323
Supports internal operations.
2424

@@ -32,9 +32,9 @@ Supports internal operations.
3232

3333
### Remarks
3434

35-
The type is a signed integer that describes an object that can store a byte offset involved in various stream positioning operations. Its representation has at least 32 value bits. It is not necessarily large enough to represent an arbitrary byte position within a stream. The value `streamoff(-1)` generally indicates an erroneous offset.
35+
The type is a signed integer. It describes an object that can store a byte offset in stream positioning operations. Its representation has at least 32 value bits. It isn't necessarily large enough to represent an arbitrary byte position within a stream. The value `streamoff(-1)` generally indicates an erroneous offset.
3636

37-
## <a name="streampos"></a> streampos
37+
## `streampos`
3838

3939
Holds the current position of the buffer pointer or file pointer.
4040

@@ -44,7 +44,7 @@ typedef fpos<mbstate_t> streampos;
4444

4545
### Remarks
4646

47-
The type is a synonym for [fpos](../standard-library/fpos-class.md)< `mbstate_t`>.
47+
The type is a synonym for [`fpos`](../standard-library/fpos-class.md)< `mbstate_t`>.
4848

4949
### Example
5050

@@ -69,7 +69,7 @@ int main( )
6969
7
7070
```
7171

72-
## <a name="streamsize"></a> streamsize
72+
## `streamsize`
7373

7474
Denotes the size of the stream.
7575

@@ -83,11 +83,11 @@ Denotes the size of the stream.
8383

8484
### Remarks
8585

86-
The type is a signed integer that describes an object that can store a count of the number of elements involved in various stream operations. Its representation has at least 16 bits. It is not necessarily large enough to represent an arbitrary byte position within a stream.
86+
The type is a signed integer that describes an object that can store a count of the number of elements involved in various stream operations. Its representation has at least 16 bits. It isn't necessarily large enough to represent an arbitrary byte position within a stream.
8787

8888
### Example
8989

90-
After compiling and running the following program, look at the file test.txt to see the effect of setting `streamsize`.
90+
After compiling and running the following program, look at the file `test.txt` to see the effect of setting `streamsize`.
9191

9292
```cpp
9393
// ios_streamsize.cpp
@@ -105,19 +105,19 @@ int main( )
105105
}
106106
```
107107
108-
## <a name="wios"></a> wios
108+
## `wios`
109109
110-
Supports the wios class from the old iostream library.
110+
Supports the `wios` class from the old `iostream` library.
111111
112112
```cpp
113113
typedef basic_ios<wchar_t, char_traits<wchar_t>> wios;
114114
```
115115

116116
### Remarks
117117

118-
The type is a synonym for class template [basic_ios](../standard-library/basic-ios-class.md), specialized for elements of type **`wchar_t`** with default character traits.
118+
The type is a synonym for class template [`basic_ios`](../standard-library/basic-ios-class.md), specialized for elements of type **`wchar_t`** with default character traits.
119119

120-
## <a name="wstreampos"></a> wstreampos
120+
## `wstreampos`
121121

122122
Holds the current position of the buffer pointer or file pointer.
123123

@@ -127,7 +127,7 @@ typedef fpos<mbstate_t> wstreampos;
127127

128128
### Remarks
129129

130-
The type is a synonym for [fpos](../standard-library/fpos-class.md)< `mbstate_t`>.
130+
The type is a synonym for [`fpos`](../standard-library/fpos-class.md)< `mbstate_t`>.
131131

132132
### Example
133133

0 commit comments

Comments
 (0)