I need to allocate a certain space in memory, and I have been using VirtualAlloc
for this.
However, I have increasingly noticed that VirtualAlloc
returns an address that exceeds 32 bits, though always less than 33 bits.
The consequence is that when I copy data to this memory address, the computer crashes into a BSOD.
I am using 64-bit windows and a 64-bit Python. I suspect that the program that copies data to the memory is only equipped to handle 32 bits. Is there is a way to enforce VirtualAlloc
to provide an address within 32 bits?
I am using Python
, specifically the ctypes
package to call VirtualAlloc
, see code below.
Executing this code multiple times changes the address, so repeatedly calling the function below will eventually result in an address below 32 bits. However, I am looking for the cause of and a fail-safe solution to the problem. Any help would be greatly appreciated.
import ctypes
mem_commit = 0x1000
page_readwrite = 0x4
size_bytes = 200000 # Allocation sizes are usually around this value
ctypes.windll.kernel32.VirtualAlloc.argtypes = [
ctypes.c_void_p, ctypes.c_long, ctypes.c_long, ctypes.c_long]
ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_int
addr = ctypes.windll.kernel32.VirtualAlloc(
0, ctypes.c_long(size_bytes), mem_commit, page_readwrite)
Note that I free up the memory afterwards using VirtualFree
.