0

我的程序很简单,

...
#define TO_INT(a) (*(int *)a)
void *pool_head;
void *pool_tail;
...

pool_head = sbrk(BUF_LENGTH);
if (pool_head == (void *)-1) {
    errno = ENOMEM;
    return pool_head;
}
pool_tail = sbrk(0);
TO_INT(pool_head)     = BUF_LENGTH * -1;
TO_INT((pool_tail - 3)) = BUF_LENGTH * -1;

当我调试它时,它显示:

Program received signal SIGSEGV, Segmentation fault.TO_INT((pool_tail - 3)) = BUF_LENGTH * -1;

pool_tail & pool_head 的值:

pool_tail = 0x805a000

pool_head = 0x804a000

如何解决?谢谢!

4

1 回答 1

2

首先,您不能对 执行指针算术 ( pool_tail - 3) void *

其次,即使我们允许它并将其pool_tail视为char *,减去pool_tail3 也不会提供足够的空间来容纳int

于 2012-06-08T01:42:22.347 回答