Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7993175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:46:25+00:00 2026-06-04T13:46:25+00:00

This code is from the K & R book – Chapter 8 Section 7:

  • 0

This code is from the K & R book – Chapter 8 Section 7: Example – Storage Allocator. This code, at least for me, doesn’t make sense. “Header” is a union of a struct and a “most restrictive alignment type”, which is a long type. Malloc will then find a big enough free space with size of a multiple of the header size.

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 *morecore(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 */

  }
}

The odd part of this code is the statement nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) + 1; which is then used in the comparison if (p->s.size >= nunits) to find a big enough space with units in terms of the size of Header. Shouldn’t the former be nunits = (nbytes+sizeof(Header)) / sizeof(Header) only? The original code would evaluate to a value less than it ought to be. What is with the +-1s? Why allocate space less than the desired.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:46:27+00:00Added an answer on June 4, 2026 at 1:46 pm

    The -1 and +1 are to account for values that aren’t multiplies of the block size.

    For example, suppose the block size is 10. If you try to use the formula n / 10 to get the number of required blocks then you would get 1 block for n = 15. This is wrong, you need 2.

    If you change the formula to be n / 10 + 1 then it will also be wrong. When n = 20 you only need 2 blocks, but that formula will return 3.

    The correct formula is (n - 1) / 10 + 1. That’s how you round up with integer division. The only difference with this and the formula you asked about is the extra sizeof(Header), which is just the extra space needed for the header itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From this code: int x = 5; int other = 10; vector<int*> v_ptr; v_ptr.push_back(&x);
I copied this code from an example . I've read it 100 times. Array.prototype.map
I get this code from a book but i could not get it work.
I study this code from some book: #include <pthread.h> #include <stdio.h> /* Parameters to
I'm trying to understand this code from Deitels 5th edition c book. If I'm
This Code is a code I built from the algorithm design manual book but
I believe there's a typo on this code snippet extracted from Stroustup's book, at
I was programming the example code from Frank Luna's book Introduction to 3D Game
This is code from the book - Programming Interviews Exposed 2nd Edition - Pages
I'm a python programmer getting to learn C from the K&R book. This will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.