c - 设置边界保护数组

标签 c windows linux mmu

我想分配一个数组并对其进行设置,使其前后的页面都受到内存管理单元的保护,因此会自动捕获超出数组边界的尝试;然后捕获它以便以受控方式处理错误。

我怀疑是否会有可移植的解决方案,但是分别在 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.

http://dmalloc.com/docs/latest/online/dmalloc_17.html#SEC22

关于c - 设置边界保护数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11076788/

相关文章:

windows - 将 IPython 配置为使用 powershell 而不是 cmd

c# - (Win/C#/.Net) Applet 需要每小时轮询一次 - 关于最佳方法的建议?

linux - 在bash中每n个命令执行一次函数

c - 在 C 中用 '%20' 替换空格

c - getint 函数出现段错误错误

c - c 中的 maketime() 改变了我的时间输入

windows - 如何创建用于绘图的子矩形设备上下文

c - Valgrind:不是堆叠的、malloc 的或(最近)free 的

linux - 将同名文件夹移动到新目录 Linux, Ubuntu

linux - 在/dev/shm 的子目录中创建共享内存时,shm_open() 失败并返回 EINVAL