我想分配一个数组并对其进行设置,使其前后的页面都受到内存管理单元的保护,因此会自动捕获超出数组边界的尝试;然后捕获它以便以受控方式处理错误。
我怀疑是否会有可移植的解决方案,但是分别在 Windows 上使用 Microsoft C 和在 Linux 上使用 GCC 的最佳平台特定方法是什么?
最佳答案
实际上,有一种可移植的方法可以检测软件中的内存分配溢出。 Dmalloc提供 fencepost 覆盖检测
Fence-post memory is the area immediately above or below memory allocations. It is all too easy to write code that accesses above or below an allocation - especially when dealing with arrays or strings. The library can write special values in the areas around every allocation so it will notice when these areas have been overwritten.
关于c - 设置边界保护数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11076788/