Writing my own malloc

Hi, I am reading K&R of C about the storage allocator.
Here they are defining malloc but i am having some problems in understanding it.

Why do they do nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) + 1;

Can anybody help me on this
Could you give some context ? We can't say what is the purpose of that line without knowing what is Header and nbytes and nunits.
Can you post the whole code ?
The statement is working out the number of blocks - of size sizeof(Header) - that you need to store the header plus the memory you've requested.

e.g. If you have a header of size 8 bytes and ask for 5 bytes, you're going to get 2 units (= 16 bytes). You'll also get 2 units if you request 8 bytes, but 3 if you request 9 bytes. Etc.

When I've seen this kind of thing before the block size has been defined separately to the header size.

Andy
Topic archived. No new replies allowed.