Skip to content

Commit d918316

Browse files
authored
Merge pull request #5510 from MicrosoftDocs/main
3/21/2024 AM Publish
2 parents ef309e4 + 512b8e1 commit d918316

9 files changed

+55
-14
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: "Compiler Error C2323"
3+
description: "Learn more about: Compiler Error C2323"
4+
ms.date: "03/20/2024"
5+
f1_keywords: ["C2323"]
6+
helpviewer_keywords: ["C2323"]
7+
---
8+
# Compiler Error C2323
9+
10+
'identifier': non-member operator `new` or `delete` functions may not be declared `static` or in a namespace other than the global namespace.
11+
12+
The `new` and `delete` overload operators must be non-static, defined in the global namespace or as class members.
13+
14+
The following generates C2323:
15+
16+
```cpp
17+
// C2323.cpp
18+
// compile with: /c
19+
static void* operator new(size_t); // C2323 since static
20+
static void operator delete(void*); // C2323 since static
21+
22+
namespace NS
23+
{
24+
void* operator new(size_t); // C2323 since not defined in the global namespace
25+
void operator delete(void*); // C2323 since not defined in the global namespace
26+
}
27+
```
28+
29+
## See also
30+
31+
[`new` and `delete` operators](../../cpp/new-and-delete-operators.md)

docs/error-messages/compiler-errors-1/compiler-errors-c2300-through-c2399.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
description: "Learn more about: Compiler errors C2300 Through C2399"
33
title: "Compiler errors C2300 Through C2399"
44
ms.date: "04/21/2019"
5-
f1_keywords: ["C2303", "C2304", "C2305", "C2306", "C2314", "C2321", "C2323", "C2328", "C2329", "C2330", "C2331", "C2335", "C2336", "C2339", "C2340", "C2342", "C2343", "C2347", "C2354", "C2358", "C2359", "C2363", "C2366", "C2367", "C2398", "C2399"]
6-
helpviewer_keywords: ["C2303", "C2304", "C2305", "C2306", "C2314", "C2321", "C2323", "C2328", "C2329", "C2330", "C2331", "C2335", "C2336", "C2339", "C2340", "C2342", "C2343", "C2347", "C2354", "C2358", "C2359", "C2363", "C2366", "C2367", "C2398", "C2399"]
7-
ms.assetid: 07ca45b5-b2f0-4049-837b-40a7a3caed88
5+
f1_keywords: ["C2303", "C2304", "C2305", "C2306", "C2314", "C2321", "C2328", "C2329", "C2330", "C2331", "C2335", "C2336", "C2339", "C2340", "C2342", "C2343", "C2347", "C2354", "C2358", "C2359", "C2363", "C2366", "C2367", "C2398", "C2399"]
6+
helpviewer_keywords: ["C2303", "C2304", "C2305", "C2306", "C2314", "C2321", "C2328", "C2329", "C2330", "C2331", "C2335", "C2336", "C2339", "C2340", "C2342", "C2343", "C2347", "C2354", "C2358", "C2359", "C2363", "C2366", "C2367", "C2398", "C2399"]
87
---
98
# Compiler errors C2300 Through C2399
109

@@ -39,7 +38,7 @@ The articles in this section of the documentation explain a subset of the error
3938
|[Compiler error C2320](compiler-error-c2320.md)|expected ':' to follow access specifier '*specifier*'|
4039
|Compiler error C2321|'*identifier*' is a keyword, and cannot be used in this context|
4140
|[Compiler error C2322](compiler-error-c2322.md)|'*identifier*': address of dllimport '*identifier*' is not static|
42-
|Compiler error C2323|'*identifier*': non-member operator new or delete functions may not be declared static or in a namespace other than the global namespace|
41+
|[Compiler error C2323](compiler-error-c2323.md)|'*identifier*': non-member operator new or delete functions may not be declared static or in a namespace other than the global namespace|
4342
|[Compiler error C2324](compiler-error-c2324.md)|'*identifier*': unexpected to the right of '::~'|
4443
|[Compiler error C2325](compiler-error-c2325.md)|'*type1*': unexpected type to the right of '->~': expected '*type2*'|
4544
|[Compiler error C2326](compiler-error-c2326.md)|'*declarator*': function cannot access '*identifier*'|

docs/error-messages/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,8 @@ items:
931931
href: compiler-errors-1/compiler-error-c2320.md
932932
- name: Compiler error C2322
933933
href: compiler-errors-1/compiler-error-c2322.md
934+
- name: Compiler error C2323
935+
href: compiler-errors-1/compiler-error-c2323.md
934936
- name: Compiler error C2324
935937
href: compiler-errors-1/compiler-error-c2324.md
936938
- name: Compiler error C2325

docs/sanitizers/error-global-buffer-overflow.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ int main(int argc, char **argv) {
126126
case 'g': return global[one * 11]; //Boom! simple global
127127
case 'c': return C::array[one * 11]; //Boom! class static
128128
case 'f':
129-
static int array[10];
130-
memset(array, 0, 10);
129+
{
130+
static int array[10] = {};
131131
return array[one * 11]; //Boom! function static
132+
}
132133
case 'l':
133134
// literal global ptr created by compiler
134135
const char *str = "0123456789";

docs/sanitizers/error-memcpy-param-overlap.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ int main(int argc, char **argv) {
3535
To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts):
3636
3737
```cmd
38-
cl example1.cpp /fsanitize=address /Zi
38+
cl example1.cpp /fsanitize=address /Zi /Oi
3939
devenv /debugexe example1.exe
4040
```
4141

42+
The [/Oi flag](../build/reference/oi-generate-intrinsic-functions.md) tells the compiler to treat `memcpy` and `memmove` as intrinsic functions. This is necessary because some versions of the standard library implement `memcpy` and `memmove` in the same way. Because ASAN is a dynamic analysis tool, it only detects errors with an observable runtime effect.
43+
4244
### Resulting error
4345

4446
:::image type="content" source="media/memcpy-param-overlap-example-1.png" alt-text="Screenshot of debugger displaying memcpy-param-overlap error in example 1.":::

docs/sanitizers/error-stack-buffer-overflow.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public:
8686

8787
class Child : public Parent {
8888
public:
89-
int extra_field;
89+
volatile int extra_field;
9090
};
9191

9292
int main(void) {
@@ -95,7 +95,7 @@ int main(void) {
9595
Child *c = (Child*)&p;
9696
c->extra_field = 42; // Boom !
9797

98-
return 0;
98+
return (c->extra_field == 42);
9999
}
100100
```
101101

docs/sanitizers/error-stack-buffer-underflow.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ int main() {
3131
To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts):
3232

3333
```cmd
34-
cl example1.cpp /fsanitize=address /Zi
34+
cl example1.cpp /fsanitize=address /Zi /Od
3535
devenv /debugexe example1.exe
3636
```
3737

38+
ASAN is a form of dynamic analysis, which means it can only detect bad code that is actually executed. An optimizer will remove the assignment to `buffer[subscript]` because `buffer[subscript]` is never read from. As a result, this example requires the `/Od` flag.
39+
3840
### Resulting error
3941

4042
:::image type="content" source="media/stack-buffer-underflow-example-1.png" alt-text="Screenshot of debugger displaying stack-buffer-underflow error in example 1.":::

docs/sanitizers/error-stack-use-after-return.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This check can slow your application down substantially. Consider the [Clang sum
2121
```cpp
2222
// example1.cpp
2323
// stack-use-after-return error
24-
char* x;
24+
volatile char* x;
2525

2626
void foo() {
2727
char stack_buffer[42];
@@ -33,7 +33,7 @@ int main() {
3333
foo();
3434
*x = 42; // Boom!
3535

36-
return 0;
36+
return (*x == 42);
3737
}
3838
```
3939

@@ -96,11 +96,13 @@ int main(int argc, char* argv[]) {
9696
To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts):
9797
9898
```cmd
99-
cl example2.cpp /fsanitize=address /fsanitize-address-use-after-return /Zi
99+
cl example2.cpp /fsanitize=address /fsanitize-address-use-after-return /Zi /Od
100100
set ASAN_OPTIONS=detect_stack_use_after_return=1
101101
devenv /debugexe example2.exe 1
102102
```
103103

104+
ASAN is a form of dynamic analysis, which means it can only detect bad code that is actually executed. An optimizer may determine that the value of `t[100 + Idx]` or `sink` is never used and elide the assignment. As a result, this example requires the `/Od` flag.
105+
104106
### Resulting error - C++ and templates
105107

106108
:::image type="content" source="media/stack-use-after-return-example-2.png" alt-text="Screenshot of debugger displaying stack-use-after-return error in example 2.":::

docs/sanitizers/error-stack-use-after-scope.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,12 @@ void main() {
147147
To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts):
148148
149149
```cmd
150-
cl example4.cpp /EHsc /fsanitize=address /Zi
150+
cl example4.cpp /EHsc /fsanitize=address /Zi /Od
151151
devenv /debugexe example4.exe
152152
```
153153

154+
ASAN is a form of dynamic analysis, which means it can only detect bad code that is actually executed. An optimizer may propagate the value of `v` in these cases instead of reading from the address stored in `p`. As a result, this example requires the `/Od` flag.
155+
154156
### Resulting error - temporaries
155157

156158
:::image type="content" source="media/stack-use-after-scope-example-4.png" alt-text="Screenshot of debugger displaying stack-use-after-scope error in example 4.":::

0 commit comments

Comments
 (0)