Skip to content

Commit da0065c

Browse files
Updated C6334
Updated to new format, clarified wording, matched spacing to my other PRs
1 parent 94848c7 commit da0065c

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

docs/code-quality/c6334.md

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,45 @@
11
---
22
description: "Learn more about: C6334"
33
title: C6334
4-
ms.date: 11/04/2016
4+
ms.date: 08/25/2022
55
ms.topic: reference
6-
f1_keywords: ["C6334"]
6+
f1_keywords: ["C6334", "SIZEOFEXPR", "__WARNING_SIZEOFEXPR"]
77
helpviewer_keywords: ["C6334"]
88
ms.assetid: 83c8abfb-b11e-4573-8c6f-95b205d32137
99
---
10-
# C6334
10+
# Warning C6334
1111

12-
> warning C6334: sizeof operator applied to an expression with an operator may yield unexpected results
12+
> sizeof operator applied to an expression with an operator may yield unexpected results
1313
14-
This warning indicates a misuse of the **`sizeof`** operator. The **`sizeof`** operator, when applied to an expression, yields the size of the type of the resulting expression.
14+
## Remarks
1515

16-
For example, in the following code:
16+
The `sizeof` operator, when applied to an expression, yields the size of the type of the resulting expression.
1717

18-
```cpp
19-
char a[10];
20-
size_t x;
21-
22-
x = sizeof (a - 1);
23-
```
24-
25-
`x` will be assigned the value 4, not 9, because the resulting expression is no longer a pointer to the array `a`, but simply a pointer.
18+
Code analysis name: SIZEOFEXPR
2619

2720
## Example
2821

29-
The following code generates this warning:
22+
The following code generates this warning. Since `a - 4` is an expression, `sizeof` will return the size of the resulting pointer, not the size of the structure found at that pointer:
3023

3124
```cpp
3225
void f( )
3326
{
34-
size_t x;
35-
char a[10];
36-
37-
x= sizeof (a - 4);
38-
// code...
27+
size_t x;
28+
char a[100];
29+
x = sizeof(a - 4);
30+
assert(x == 96); //assert fails since x == sizeof(char*)
3931
}
4032
```
4133
42-
To correct this warning, use the following code:
34+
To correct this warning, ensure you are working with the return value of `sizeof`, not the argument to it:
4335
4436
```cpp
4537
void f( )
4638
{
47-
size_t x;
48-
char a[10];
49-
50-
x= sizeof (a) - 4;
51-
// code...
39+
size_t x;
40+
char a[100];
41+
x = sizeof(a) - 4;
42+
assert(x == 96); //assert succeeds
5243
}
5344
```
5445

0 commit comments

Comments
 (0)