K& R C示例malloc - moreroce指针

时间:2017-04-27 22:58:18

标签: c memory malloc

在K& R&C中,C编程语言提供了malloc的实现:

static Header base; /* empty list to get started */
static Header *freep = NULL; /* start of free list */
/* malloc: general-purpose storage allocator */
void *malloc(unsigned nbytes){
  Header *p, *prevp;
  Header *moreroce(unsigned);
  unsigned nunits;
  nunits = (nbytes+sizeof(Header)-1)/sizeof(header) + 1;
  if ((prevp = freep) == NULL) { /* no free list yet */
    base.s.ptr = freeptr = prevptr = &base;
    base.s.size = 0;
  }
  for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
    if (p->s.size >= nunits) { /* big enough */
        if (p->s.size == nunits) /* exactly */
            prevp->s.ptr = p->s.ptr;
        else { /* allocate tail end */
            p->s.size -= nunits;
            p += p->s.size;
            p->s.size = nunits;
        }
        freep = prevp;
        return (void *)(p+1);
    }
    if (p == freep) /* wrapped around free list */
    if ((p = morecore(nunits)) == NULL)
    return NULL; /* none left */
  }
}

我对这条线感到困惑:  标题* moreroce(未签名); 这条线是什么?指针moreroce不用于任何东西,(unsigned)是什么意思? 感谢。

1 个答案:

答案 0 :(得分:3)

moreroce是一个错字。

Header *morecore(unsigned);

是名为morecore的函数的声明,它以unsigned为参数。