问题
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