Error remained in Visual C++ 2019 16.9.2: 32 bit pointers in 64 bit programs: reads 64 bits from memory

Zoltán Hegedüs 101 Reputation points
2021-03-24T18:57:43.317+00:00

My English is not perfect.
Visual C++ 2019 16.9.2.
I announced an error a long time ago, I do not find this. MS wrote will be fixed in a later version. But remained:

#include <iostream>

using namespace std;

int x = 1, y = 2;
int * __ptr32 (a[2]) = {&x, &y};
void f(int const &z) { wcout << &z << endl; }

int wmain() {
 f(*(a[0]));
 int &z = *(a[0]);
 int *p = a[0];
 wcout << &z << endl << p << endl << &y << endl;
 return 0;
}

In a 64 bit program, when a 32 bit pointer is passed to a function, as the address of a reference parameter, reads 64 bit from memory. So, the address will be bad. The program crashes if there is z instead of &z in wcout in the function f. This program requires disabled LARGE_ADDRESS_AWARE among linker's settings. This causes the program can use only 2 GB address space: 0 to 2^31-1 addresses. Debug and Release mode are concerned so. AMD64 is concerned, I have no information about ARM64.

32 bit pointers in 64 bit program useful if the program uses a lot of pointers: lists, trees, etc. This can halve memory need. 64 bit mode required to processor test/benchmark program, what must use 64 bit dlls. In 64 bit mode, there are 16 registers instead of 8, so faster. Memory comb, fill, copy, exchange, compare, arithmetic operations on big numbers has double speed than 32 bit mode.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,527 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. metablaster 91 Reputation points
    2021-03-24T21:18:45.173+00:00

    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.

    https://learn.microsoft.com/en-us/cpp/cpp/ptr32-ptr64?view=msvc-160

    I don't any issue here, the documentation is clear enough that __ptr32 (32 bit pointer) will be 64 bit pointer in x64 application.

    32 bit pointers in 64 bit program useful if the program uses a lot of pointers: lists, trees, etc. This can halve memory need

    You may gain some memory but very far from halve the memory used, and not for free, there will be performance overhead.