From 5175ad67713c4c368a8aa9b707b0dec0444dad01 Mon Sep 17 00:00:00 2001 From: jdp_ <42700985+jdpatdiscord@users.noreply.github.com> Date: Mon, 8 Dec 2025 12:22:53 -0500 Subject: [PATCH 1/2] add clarification to example --- docs/cpp/ptr32-ptr64.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cpp/ptr32-ptr64.md b/docs/cpp/ptr32-ptr64.md index 08fc2cedc23..065deb056e2 100644 --- a/docs/cpp/ptr32-ptr64.md +++ b/docs/cpp/ptr32-ptr64.md @@ -28,7 +28,7 @@ For compatibility with previous versions, **_ptr32** and **_ptr64** are synonyms ## Example -The following example shows how to declare and allocate pointers with the **`__ptr32`** and **`__ptr64`** keywords. +The following example shows how to declare and allocate pointers with the **`__ptr32`** and **`__ptr64`** keywords. This code will crash when compiled for 64-bit due to the pointer from `malloc` being truncated to 32-bit. Since 32-bit pointers can be represented in 64-bit, when compiled for 32-bit this code does not necessarily crash. ```cpp #include From 5b6b51bfddc719dbbdb81b070c22e342e374aac3 Mon Sep 17 00:00:00 2001 From: Tyler Whitney Date: Tue, 16 Dec 2025 16:43:22 -0800 Subject: [PATCH 2/2] Revise ptr32 and ptr64 documentation Updated to address Copilot suggestions. --- docs/cpp/ptr32-ptr64.md | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/docs/cpp/ptr32-ptr64.md b/docs/cpp/ptr32-ptr64.md index 065deb056e2..a41c67307da 100644 --- a/docs/cpp/ptr32-ptr64.md +++ b/docs/cpp/ptr32-ptr64.md @@ -1,16 +1,17 @@ --- description: "Learn more about: __ptr32, __ptr64" title: "__ptr32, __ptr64" -ms.date: "10/09/2018" +ms.date: 12/16/2025 f1_keywords: ["__ptr32_cpp", "__ptr64_cpp", "__ptr32", "__ptr64", "_ptr32", "_ptr64"] helpviewer_keywords: ["__ptr64 keyword [C++]", "_ptr32 keyword [C++]", "ptr32 keyword [C++]", "ptr64 keyword [C++]", "_ptr64 keyword [C++]", "__ptr32 keyword [C++]"] -ms.assetid: afb563d8-7458-4fe7-9c30-bd4b5385a59f --- # __ptr32, __ptr64 -**Microsoft Specific** +**Microsoft specific** -**`__ptr32`** represents a native pointer on a 32-bit system, while **`__ptr64`** represents a native pointer on a 64-bit system. +Use **`__ptr32`** to represent a native pointer on a 32-bit system. Use **`__ptr64`** to represent a native pointer on a 64-bit system. + +The `__ptr32` and `__ptr64` modifiers are Microsoft-specific extensions for interop scenarios. For standard 32-bit (x86) or standard x64 code, use native pointers, instead. The following example shows how to declare each of these pointer types: @@ -19,16 +20,21 @@ int * __ptr32 p32; int * __ptr64 p64; ``` -On a 32-bit system, a pointer declared with **`__ptr64`** is truncated to a 32-bit pointer. On a 64-bit system, a pointer declared with **`__ptr32`** is coerced to a 64-bit pointer. +On a 32-bit system, a pointer declared with **`__ptr64`** is treated as a 32-bit pointer and truncates the upper 32 bits of any 64-bit address assigned to it. On a 64-bit system, a pointer declared with **`__ptr32`** is treated as a 32-bit pointer and truncates the upper 32 bits of any 64-bit address assigned to it. This truncation can lead to an invalid pointer if the 64-bit address is above 4GB. > [!NOTE] -> You cannot use **`__ptr32`** or **`__ptr64`** when compiling with **/clr:pure**. Otherwise, Compiler Error C2472 will be generated. The **/clr:pure** and **/clr:safe** compiler options are deprecated in Visual Studio 2015 and unsupported in Visual Studio 2017. +> You can't use **`__ptr32`** or **`__ptr64`** when compiling with **`/clr:pure`**. Otherwise, the compiler generates error C2472. The **/clr:pure** and **/clr:safe** compiler options are deprecated in Microsoft Visual Studio 2015 and unsupported in Microsoft Visual Studio 2017. -For compatibility with previous versions, **_ptr32** and **_ptr64** are synonyms for **`__ptr32`** and **`__ptr64`** unless compiler option [/Za \(Disable language extensions)](../build/reference/za-ze-disable-language-extensions.md) is specified. +For compatibility with previous versions, **`_ptr32`** and **`_ptr64`** are synonyms for **`__ptr32`** and **`__ptr64`** unless you specify compiler option [/Za \(Disable language extensions)](../build/reference/za-ze-disable-language-extensions.md). ## Example -The following example shows how to declare and allocate pointers with the **`__ptr32`** and **`__ptr64`** keywords. This code will crash when compiled for 64-bit due to the pointer from `malloc` being truncated to 32-bit. Since 32-bit pointers can be represented in 64-bit, when compiled for 32-bit this code does not necessarily crash. +The following example shows how to declare and allocate pointers with the **`__ptr32`** and **`__ptr64`** keywords. + +This code works on x86 but might crash on x64. + +- It works when compiled for 32-bit because **`__ptr64`** pointers are treated as 32-bit pointers on x86. On x86 (32-bit), `malloc` returns a 32-bit address which fits in `p64`. +- It might crash when compiled for 64-bit because on x64, `malloc` returns a 64-bit pointer which is truncated to 32 bits by this line: `p32 = (int* __ptr32)malloc(4);`. Truncating a 64-bit address to a 32-bit address could result in an invalid pointer if the allocation happened above 4GB. In that case, `*p32 = 32` could attempt to access a truncated address that isn't part of your process's address space, causing an access violation. Even if it works once, it could fail later if the memory allocator returns a higher address. ```cpp #include @@ -38,22 +44,22 @@ int main() { using namespace std; - int * __ptr32 p32; - int * __ptr64 p64; + int* __ptr32 p32; + int* __ptr64 p64; - p32 = (int * __ptr32)malloc(4); - *p32 = 32; + p64 = (int* __ptr64)malloc(4); + *p64 = 64; // Works on x86 and x64 + cout << *p64 << endl; + + p32 = (int* __ptr32)malloc(4); + *p32 = 32; // Works on x86. Possible exception on x64 cout << *p32 << endl; - - p64 = (int * __ptr64)malloc(4); - *p64 = 64; - cout << *p64 << endl; } ``` ```Output -32 64 +32 ``` **END Microsoft Specific**