Search code examples
pythonwindowspython-2.6wow64

What is the highest number Python 2.* x86 (i.e.32bit) id() function on Windows x64 can return?


What is the highest number Python 2.6's x86 id() function can return?

I presume the ceiling must be the amount of memory any 32-bit application can see which must be: 232, i.e. 4294967296?

(Which would be fine as I have to fit this value in a C# type for which UInt32 or the CLS-compliant Int64 would suffice, which it the reason for my concern, though irrelevant to the question.)

But what if a I am running on Windows x64 with more than 2GB of memory say 32GB of memory - is it possible for Python's id() function, even though the Python interpreter itself is x86, to return a value higher then 232???

I am guessing it comes down to the Python interpreter's view of the machine - I imagine WoW64 translates 64-bit memory addresses to 32-bit addresses - but I am only guessing - I need to be sure!


Solution

  • Assuming the documentation for id() is correct and CPython (x86) returns the address of the object, the maximum value that can be returned is 232-1 (4294967295). This can be demonstrated in a simple C program:

    #include <stdio.h>
    
    int main(void) {
        void* p = 0;          // specify a pointer at 0
        p = ~p;               // invert all bits
        uintptr_t x = p;      // convert to an integer type fo the same size
        printf("%lu\n", x);
        return 0;
    }
    

    This is true regardless of the underlying OS, an application compiled with a 32-bit pointer type can only specify addresses between 0 and 232-1. Note that the address visible in the application may not correspond to a particular physical memory address, due to the use of virtual memory.