Realloc implementation

Realloc implementation

Ask Question

10 years, 6 months ago

10 years, 6 months ago

1k times



I’m writing a simple linked list based memory manager in the form:

…Header|Block|Header|Block… with a used and free list.

If the realloc() function was asked to reduce the size of a block, is it okay to overwrite some of the trailing bytes with the header for the newly created block? The documentation I’ve read suggests this is ‘undefined behaviour’ but do applications depend on the data still being there?

c memory realloc

share|improve this question

edited Sep 2 ’09 at 4:46


65.2k4141 gold badges160160 silver badges222222 bronze badges

asked Sep 2 ’09 at 4:43


24922 silver badges1212 bronze badges

add a comment

2 Answers





Most likely, the remark on undefined behavior went like this: “it is undefined to access any bytes after the end of the block when realloc returns”.

Such a specification is there precisely to allow you to put a header into the trailing bytes in the implementation of realloc. That it is undefined behavior means that if an application would try to read from the bytes (that are conceptually gone), it would read your header, which would appear as garbage to the application; if it even writes, it would kill your header – so the application shouldn’t do that.

share|improve this answer

answered Sep 2 ’09 at 4:52

Martin v. LöwisMartin v. Löwis

106k1616 gold badges177177 silver badges223223 bronze badges

add a comment


Sure. It has been reallocated, so now it has been released by the app and it belongs to your manager. It’s normal to start clobbering it with new pointers.

share|improve this answer

answered Sep 2 ’09 at 4:50


129k2323 gold badges220220 silver badges304304 bronze badges

add a comment

Your Answer

Thanks for contributing an answer to Stack Overflow!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password


Post as a guest


Required, but never shown

Post Your Answer


By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you’re looking for? Browse other questions tagged c memory realloc or ask your own question.

The Overflow Blog

Defending yourself against coronavirus scams

Podcast Episode 220: Fully Remote

Featured on Meta

Planned maintenance scheduled for Saturday, March 28, 2020 at 13:00 UTC (9AM…

An Update On Creative Commons Licensing

Community and Moderator guidelines for escalating issues via new response…

How does the Triage queue work?

Triage needs to be fixed urgently, and users need to be notified upon…

Visit chat


6Custom malloc() implementation header design2Reallocating an array (C99)0C program crashes by calling realloc() several times1Different realloc behaviour in linux and osx7Custom malloc implementation0Using array of char to pad struct in C

Hot Network Questions

How can I reproduce this “minimalist” background?

I was up a piece but could not win. What went wrong?

How does FM station have fixed frequency when FM modulation changes the frequency?

Did astronaut Scott Kelley eat cake in space?

Why would a government support organizations with completely opposing goals?

How can I avoid losing or missing a SIM ejector tool?

What should I call a “differential” which cubes, rather than squares, to zero?

Cook me a character meal

How much of the US media referred to the COVID-19 as the “Wuhan Virus” until it received an official designation from the WHO?

How do “tip jet” helicopters cancel the torque effect of the main rotor?

Can a creature take turns as normal if they are inside an Antimagic Field while another creature casts Time Stop?

When we press a piano key, why does a high pitch note not run for as long as a low pitch note?

What does “She ran up the stairs two at a time.” mean?

What’s the point of Super Star Destroyers?

Autoroute or not?

Why is paragliding not an old invention?

Why are many African-Americans in Mississippi concentrated in the northwestern area?

When filing taxes, how do I report “informal income” for which I don’t have any forms?

How much project management is a software developer supposed to do?

A colorful metapuzzle (of a sort)

Force Windows to install *all* missing updates

Is it possible to use global IP addresses without internet?

How to make bread with plain flour?

Why is it funny to say Giri played the Evans Gambit?

more hot questions

Question feed

Subscribe to RSS

Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.


Leave a Reply

Your email address will not be published. Required fields are marked *