检查指针是否在malloc'd区域中? [英] Checking if a pointer is in a malloc'd area?

查看:114
本文介绍了检查指针是否在malloc'd区域中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个动态内存分配器,我需要检查释放它的一部分时是否确实要传递给函数的指针确实在该区域内.我有一个指向malloc'd区域开始的指针

I'm making a dynamic memory allocator and I need to check than when I'm freeing a section of it, that the pointer I'm passing into the function is in fact within that area. I have a pointer to the beginning of the malloc'd area

typedef unsigned char byte;

static byte *memory // pointer to start of allocator memory

我在启动函数中分配的

.我还拥有存储在其中的malloc区域的大小

which I assign in my initiating function. I also have the size of the malloc'd area stored in

static u_int33_t memory_size;   // number of bytes malloc'd in memory[]

如何确保ptr不是...(用伪代码)

How do I ensure that the ptr isnt... (in pseudo code)

ptr < *memory || ptr > *memory + memory_size

并且该代码导致以下错误;

and that code results in the following error;

错误:不同指针类型的比较缺少强制转换[-Werror] if(对象<内存||对象>(内存+ memory_size)) ^

error: comparison of distinct pointer types lacks a cast [-Werror] if ( object < memory || object > (memory + memory_size)) ^

我不确定我需要投射什么,不需要...

I'm not sure what I need to cast and what not to...

免费功能如下...

void memfree(void *object)
{
   if ( object < memory || object > (memory + memory_size)) {
  fprintf(stderr, "vlad_free: Attempt to free via invalid pointer\n");
  exit(EXIT_FAILURE);
   }
}

推荐答案

原始

Raymond Chen的答案

Original

This is wrong as pointed out by Raymond Chen's answer

void memfree(void *_object)
{
   byte* object = (byte*)_object;
   if ( object >= memory && object < (memory + memory_size)) {
       /* defined guarantees - they happened to be in the same object. */
   } else {
      fprintf(stderr, "memfree: Attempt to free via invalid pointer\n");
      exit(EXIT_FAILURE);
   }
}

指针的类型必须相同.鉴于您似乎有一个字节范围,所以最好使用字节.

The types of pointers need to be the same. Given that you seem to have a range in bytes, it seems best to use byte.

根据C标准 n1570

比较两个指针时,结果取决于指针中的相对位置 指向对象的地址空间.如果两个指向对象类型的指针都指向 相同的对象,或者都指向同一数组对象的最后一个元素,它们 比较相等.如果指向的对象是同一聚合对象的成员, 指向稍后声明的结构成员的指针比指向成员的指针大 在结构的较早位置声明,并指向具有较大下标的数组元素的指针 值比较下标值较低的指针指向同一数组元素的指针更大.指向同一个联合对象的成员的所有指针比较相等.如果 表达式P指向数组对象的元素,表达式Q指向数组 同一数组对象的最后一个元素,指针表达式Q + 1比较大于 P.在所有其他情况下,行为都是不确定的.

When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.

在这里,我相信未定义的行为是,您无法确定在两个系统上是否存在一致的排序,因此使用a < b的代码在某些系统上可能为true,而在其他系统上为false.

Here, I believe the undefined behavior, is that you can't tell if on 2 systems, that there will be a consistent ordering, so code which uses a < b, may be true on some systems and false on others.

使用

void memfree(void *_object)
{
   uintptr_t object = (uintptr_t)_object;
   if ( object >= (uintptr_t)memory && object < ((uintptr_t)memory + (uintptr_t)memory_size)) {
       /* defined guarantees - they happened to be in the same object. */
   } else {
      fprintf(stderr, "memfree: Attempt to free via invalid pointer\n");
      exit(EXIT_FAILURE);
   }
}

这篇关于检查指针是否在malloc'd区域中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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