Question

I post this here as a last resort. I'm completely stuck and do not know where to take this. With this question, I'm looking for direction rather than the answer.

I've got a home work assignment that actually appears to be a pretty common assignment. The question is here at page 11. This isn't my assignment but it is one I found on Google that is incredibly similar to mine.

My instructor has given the guidance that the relevant piece of information we need to look at is the header bits.

Supposedly we are executing the following: free(0x400b010)

0x400b010 contains 0x400b601c.

If I break out this hex to binary -- in particular the last double word, I get the following

0x1c = 0001 1100

The entire thing:

0x400b601c = 0100 0000 0000 1011 0110 0000 0001 1100

From what I can figure, the 1c would indicate that this memory is already free. Because the last bit is 0. In this case, the behavior of free would be undefined.

But is my train of though accurate? Am I even taking this in the right direction?

Was it helpful?

Solution

You are always supposed to pass free() a pointer obtained from malloc or one of its relatives. Think for a moment about how you use those pointers.

Questions to ask yourself:

  • Can the pointer that malloc returns point to the header? To the footer? Why or why not?
  • Are you looking at header contents above?
  • Where might the header be?

BTW-- the linked document describes only one of several ways a heap allocator might function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top