views:

169

answers:

1

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?

+3  A: 

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.

dmckee
I think I see your point, thank you. In regards to your "BTW", the book being used only goes over one method: Implicit Free Lists. It mentions the other methods but very little attention is given to them so I'm assuming this implicit free lists. (At least, I think this is what you were referring to.)
Frank V
So, given what you are saying -- that `0x400b601c` is the value being pointed at as the starting point, the header value would be below (heap grows up) this. This would mean that we don't know what the header says... unless `0x400b00c` is the header....
Frank V
"the header value would be below (heap grows up) [the vector passed to free]". Yes.
dmckee
"This would mean that we don't know what the header says... unless 0x400b00c is the header"--You know where the header *ends*, and you know how big it is, yes? So you *do* know where the header is. And that should put you on the right track. Good luck.
dmckee