Porting Win32 code to 64 bit: watch out for pointers

During the porting of an application to 64 bit, I recently found out about a very interesting registry key value, that, when set, will force the Windows memory manager to give you a high address (greater than 32 bit).  This is useful if you’re dealing with a lot of code that may inadvertently store pointers in 32 bit values.  Normally you’d get a 32 bit pointer in a 64 bit program, unless you specifically ask for it using the MEM_TOP_DOWN flag for VirtualAlloc API, but there is a way to force this behavior upon all apps:

In your registry, under:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Memory Management

Just create a DWORD value named AllocationPreference and set it to hexadecimal value 100000.   (hex value 0x100000)

Even if you’ve been super careful writing code, for example, using DWORD_PTR instead of DWORD everywhere it matters, you’ll be surprised how quickly you’ll find previously hidden problems lurking in your code.  I recommend that this value be set on all of your QA team’s machines as well.    The site I link to above gives a great explanation as to what the key value actually does.  You can find more information from Microsoft about the value here:




About tedwvc
On this blog you'll find some tips and tricks for dealing with Visual C++ issues.

2 Responses to Porting Win32 code to 64 bit: watch out for pointers

  1. brucedawson says:

    Note that you need to reboot for this flag to take effect. Note also that this flag, which is equivalent to passing the MEM_TOP_DOWN to VirtualAlloc, can slow down allocations because MEM_TOP_DOWN has O(n^2) performance, where ‘n’ is the number of virtual allocations using the flag.


    I 100% agree on the importance of forcing your allocations above the 4 GB line in order to flush out pointer truncation bugs. We have used a different technique to get the same effect, documented here:


    The technique we use has the advantage of not requiring a reboot and not being system global, but it does have its own set of minor disadvantages.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: