如何确定 MSVC C++ 中的内存分配?

How to make memory allocation in MSVC C++ deterministic?

在调试一些带有大量指针的 C++ 代码时,如果运行之间的内存地址相同将会很有用。有没有办法使执行相同内存分配的程序的连续运行之间返回的一系列地址具有确定性?

也许是环境变量或可以为调试堆设置的东西?

我知道有很多很好的理由希望对发布版本进行随机化,但确定性在某些情况下对于调试很方便(例如,在修改图形时某些东西没有正确链接)。

there is a special debug heap that is used that performs extra checks and writes special values.

不,没有像调试堆这样的东西,而是调试堆管理器准备布局和金丝雀。 额外检查特殊值只是在调试模式下编译代码的结果。从操作系统获得的地址仍然是任意的。

Is there any way to make the series of addresses that are returned between consecutive runs of a program that perform the same memory allocations deterministic?

不,无论您是 运行 调试版还是发布版,都无法获得重复执行程序的相同地址。

(从评论转换而来)

您可能想要替换 operator new。在您自己的版本中,在固定基地址处创建单个大内存映射。在 64 位地址 space 中空闲的可能性 >99%。然后从这个块开始顺序分配。