From: yaojiaxing on
i found this function in the c programming language 2nd edition p187
i do not know the sentence " nunits =
(nbytes+sizeof(Header)-1)/sizeof(header) + 1;",
should minus by one.any help will be appreciated ,the function as below


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 */
}
}



--
****************************
������Զ����Զ�����е����ӡ�
����ãã�˺�֮���ҵ�Ů��
****************************


From: Richard Heathfield on
yaojiaxing said:

> i found this function in the c programming language 2nd edition p187
> i do not know the sentence " nunits =
> (nbytes+sizeof(Header)-1)/sizeof(header) + 1;",
> should minus by one.any help will be appreciated ,the function as below

(nbytes + sizeof(Header) - 1) / sizeof(Header) calculates the number of
complete objects of type Header that would be needed to fill nbytes bytes
of storage.

(You get a clue about this, actually, because the left operand of the
assignment is called nunits.)

The + 1 is because, after the allocation, the first sizeof(Header) bytes of
the allocation are used for storing size information.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
From: yaojiaxing on
(nbytes + sizeof(Header) - 1) / sizeof(Header) ,here why should minus by
one,
i think (nbytes + sizeof(Header) ) / sizeof(Header) is right,can you tell
me???
3QU

--
****************************
??????????????
????????????
****************************
"Richard Heathfield" <rjh(a)see.sig.invalid>
??????:pOmdnRKxF6SY8ufanZ2dnUVZ8h-dnZ2d(a)bt.com...
> yaojiaxing said:
>
>> i found this function in the c programming language 2nd edition p187
>> i do not know the sentence " nunits =
>> (nbytes+sizeof(Header)-1)/sizeof(header) + 1;",
>> should minus by one.any help will be appreciated ,the function as below
>
> (nbytes + sizeof(Header) - 1) / sizeof(Header) calculates the number of
> complete objects of type Header that would be needed to fill nbytes bytes
> of storage.
>
> (You get a clue about this, actually, because the left operand of the
> assignment is called nunits.)
>
> The + 1 is because, after the allocation, the first sizeof(Header) bytes
> of
> the allocation are used for storing size information.
>
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> "Usenet is a strange place" - dmr 29 July 1999


From: Richard Heathfield on
yaojiaxing said:

> (nbytes + sizeof(Header) - 1) / sizeof(Header) ,here why should minus by
> one,
> i think (nbytes + sizeof(Header) ) / sizeof(Header) is right,can you tell
> me???

When you divide an integer by an integer, any remainder is lost.

Programming 101, Jan 2008 - mid-year examination (continued)

Q739: You have B bottles of wine, and you want to get some wine-racks on
which to store them, but you don't want to spend any money unnecessarily.
A wine-rack can store R bottles. B and R are integers. Derive an
expression for the *minimum* number of wine racks you need to buy such
that you can store all the bottles on racks.

Hmmm, let's see. Obviously it should be B / R. Let's test that. Let's say
we have 100 bottles and 9 bottles per rack. 100 / 9 is 11 (remainder is
discarded). But 11 racks * 9 bottles per rack will only hold 99. So let's
try (B + 1) / R. That works for 100 and 9, but what if I had 101 bottles?
Oh dear. AHA! (B + R) / R ! That'll do it, won't it? Let's see - (101 + 9)
/ 9 = 110/9 = 12, and 12 racks will store 108 bottles. Oh, but wait. What
if I have exactly 108 bottles? Then (B + R) / R comes to 117/9 which is
13, which is 1 too many. In fact, what I need is... ah! If the number of
bottles is an exact multiple of the rack size, I want B / R, otherwise I
want (B + R) / R. Now, integer division is lossy - to increase the
quotient by 1, I need to increase the dividend by the divisor. So if I use
(B + R - 1) / R, that has exactly the effect I want. 107 bottles -> (107 +
9 - 1) / 9 = 115 / 9 = 12. 108 bottles -> (108 + 9 - 1) / 9 = 116 / 9 =
12. For 109 bottles, I'll need an extra rack. Does that work? (109 + 9 -
1) / 9 = 117 / 9 = 13, yep.

A739: (B + R - 1) / R

HTH. HAND.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
From: yaojiaxing on
with your help , i think i get it.originally,it is a algorithm.
thanks a lot.
i will have a good day i think.

--
****************************
??????????????
????????????
****************************
"Richard Heathfield" <rjh(a)see.sig.invalid>
??????:kt6dnZhitdg8FebanZ2dnUVZ8vqdnZ2d(a)bt.com...
> yaojiaxing said:
>
>> (nbytes + sizeof(Header) - 1) / sizeof(Header) ,here why should minus by
>> one,
>> i think (nbytes + sizeof(Header) ) / sizeof(Header) is right,can you tell
>> me???
>
> When you divide an integer by an integer, any remainder is lost.
>
> Programming 101, Jan 2008 - mid-year examination (continued)
>
> Q739: You have B bottles of wine, and you want to get some wine-racks on
> which to store them, but you don't want to spend any money unnecessarily.
> A wine-rack can store R bottles. B and R are integers. Derive an
> expression for the *minimum* number of wine racks you need to buy such
> that you can store all the bottles on racks.
>
> Hmmm, let's see. Obviously it should be B / R. Let's test that. Let's say
> we have 100 bottles and 9 bottles per rack. 100 / 9 is 11 (remainder is
> discarded). But 11 racks * 9 bottles per rack will only hold 99. So let's
> try (B + 1) / R. That works for 100 and 9, but what if I had 101 bottles?
> Oh dear. AHA! (B + R) / R ! That'll do it, won't it? Let's see - (101 + 9)
> / 9 = 110/9 = 12, and 12 racks will store 108 bottles. Oh, but wait. What
> if I have exactly 108 bottles? Then (B + R) / R comes to 117/9 which is
> 13, which is 1 too many. In fact, what I need is... ah! If the number of
> bottles is an exact multiple of the rack size, I want B / R, otherwise I
> want (B + R) / R. Now, integer division is lossy - to increase the
> quotient by 1, I need to increase the dividend by the divisor. So if I use
> (B + R - 1) / R, that has exactly the effect I want. 107 bottles -> (107 +
> 9 - 1) / 9 = 115 / 9 = 12. 108 bottles -> (108 + 9 - 1) / 9 = 116 / 9 =
> 12. For 109 bottles, I'll need an extra rack. Does that work? (109 + 9 -
> 1) / 9 = 117 / 9 = 13, yep.
>
> A739: (B + R - 1) / R
>
> HTH. HAND.
>
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> "Usenet is a strange place" - dmr 29 July 1999
>