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

有些话、适合烂在心里 提交于 2020-01-14 14:51:10

问题


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).


回答1:


(Converted from comment)

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.




回答2:


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

No, there's no such thing like a debug heap, but a debug heap manager that prepares layout and canaries. The extra checks and special values are just outcome of the code compiled in debug mode. Addresses are still arbitrary as gained from the operating system.

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?

No there's no way to obtain the same addresses for repeated executions of your program, no matter if you're running a debug or release build.



来源:https://stackoverflow.com/questions/35024481/how-to-make-memory-allocation-in-msvc-c-deterministic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!