4

Working on porting a 32bit Windows C++ app to 64 bit. Unfortunately, the code uses frequent casting in both directions between DWORD and pointer values.

One of the ideas is to reserve the first 4GB of virtual process space as early as possible during process startup so that all subsequent calls to reserve memory will be from virtual addresses greater than 4 GB. This would cause an access violation error any unsafe cast from pointer to DWORD and then back to pointer and would help catch errors early.

When I look at the memory map of a very simple one line C++ program, there are many libraries loaded within bottom 4GB? Is there a way to make sure that all libraries, etc get loaded only above 4GB?

Thanks

4

5 回答 5

6

使用 switch 编译您的项目/Wp64(检测 64 位可移植性问题)并修复所有警告。

于 2009-07-22T22:10:00.930 回答
4

作为一名程序员,在迁移到 64 位 Windows 时我需要担心什么?

于 2009-07-22T23:14:14.417 回答
2

VirtualAlloc()您可以在应用程序中尽早插入调用,以在较低的 4GB 中分配内存。如果使用该MEM_RESERVE参数,则只分配虚拟内存空间,因此只会使用非常少量的实际 RAM。

但是,这只会帮助您从堆中分配内存 - 程序中的任何静态数据都将在 WinMain() 之前分配,因此您将无法更改它的位置。

(顺便说一句,即使您可以在加载主二进制文件之前保留内存,我认为主二进制文件也需要在特定地址加载 - 除非它是作为与位置无关的可执行文件构建的。)

于 2009-07-22T22:20:42.670 回答
2

Bruce Dawson 发布了一种保留底部 4 GB VM 的技术的代码:

https://randomascii.wordpress.com/2012/02/14/64-bit-made-easy/

它使用 保留大部分地址空间(不是实际内存)VirtualAlloc,然后使用 处理进程堆HeapAlloc,并使用 结束 CRT 堆malloc。它简单、快速且效果很好。在我的机器上,它进行了大约 3.8 GB 的虚拟分配和只有 1 MB 的实际分配。

第一次尝试时,我立即在我正在处理的项目中发现了一个长期存在的错误。强烈推荐。

于 2015-03-23T21:20:16.480 回答
1

最好的解决方案是修复这些演员表......

无论如何,您都可以避免使用它截断指针(与强制转换为 POINTER_32 相同),因为我相信 Windows 无论如何都支持您的应用程序使用较低的 4GB。但是,这绝不能保证。你真的最好解决这些问题。

搜索“(DWORD)”的代码并修复您找到的任何内容。没有更好的解决方案...

本质上,您要求的是在启用 AWE 的 32 位内存模式下运行 64 位代码(即失去 64 位的所有真正优势)。我认为微软不会为了这么少的收益而为提供这个而烦恼……谁能责怪他们呢?

于 2009-07-22T22:06:56.083 回答