Quantcast
Viewing all articles
Browse latest Browse all 10

What happens if I realloc to smaller size?

I'm currently studying dynamic memory allocation and I've got a question on realloc function.I've learned that the contents of the newly allocated block should be same as those of the old block, up to the minimum of the old and new size, and everything else is uninitialized.

What if the heap block is using boundary tag, and I am reallocating to smaller size?

What I'm asking is, for example, here's a 24-bytes-sized memory block whose first 4 bytes are the header, 16 bytes in the middle are payload, and last 4 bytes are the footer. And I've shrinked this block to 16 bytes, whose first 4 bytes will be header of new block and last 4 bytes will be footer of new block.

First, does header change? Header should be including information about block size and alloc bit, but contents of new block should be same. Does this mean all contents including block size information in header remain same, which seems totally useless for me?

Secondly, how should I put the footer of new block? The footer should take place at 13th ~ 16th bytes of new block. But the contents of new and old block should be the same up to 16 bytes, which is violated if I put the footer at 13th ~ 16th bytes!

Sorry for such a wordy question, but I'm very very confused right now.Hopefully I could find an answer.


additional info about question)

I am currently working on malloc lab from CS:APP, which wants me to design an allocator by writing my own version of malloc, free, and realloc routines. And it says these functions should match the semantics of corresponding libc malloc, free, and realloc routines.

Right now I'm actually implementing my own realloc function, and I became confused about those situations. So I hope I can get C standard library implementation level answer.


Viewing all articles
Browse latest Browse all 10

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>