如何使MSVC C ++中的内存分配具有确定性? [英] How to make memory allocation in MSVC C++ deterministic?

查看:76
本文介绍了如何使MSVC C ++中的内存分配具有确定性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用大量指针调试某些C ++代码时,如果两次运行之间的内存地址相同,将很有用.有什么方法可以确定确定执行相同内存分配的程序的连续运行之间返回的地址系列吗?

While debugging some C++ code with tons of pointers it would be useful if the memory addresses between runs were the same. 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?

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

Maybe an environment variable or something that can be set for the debug heap?

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

I am aware that there are many good reasons you want randomization for release builds, but determinism is handy for debugging in some situations (e.g. something is not getting linked up correctly while modifying a graph).

推荐答案

(从评论转换)

您可能要替换新的运算符.在您自己的版本中,在固定的基地址上创建单个大内存映射.在64位地址空间中,有超过99%的机会是免费的.然后,只需从该块按顺序分配即可.

You may want to replace operator new. In your own version, create a single large memory mapping at a fixed base address. The chances are >99% of it being free in a 64 bits address space. Then just allocate sequentially from this block.

这篇关于如何使MSVC C ++中的内存分配具有确定性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆