Skip to main content
1 of 2
200_success Mod
  • 145.7k
  • 4
  • 114
  • 284

Cody Gray's answer on Implementing realloc in C left a good impression on me.

It started out simple enough, with the usual remarks about formatting, braces, and declarations. Then it gets more insightful:

  • The call to malloc() is buggy.
  • The call to memset() is unnecessary.
  • The entire my_realloc() function is buggy, and in fact impossible to write without access to the internals of the corresponding malloc function.

Then, there is a heavily commented suggested implementation that explains in detail how it works.

The answer concludes with a general discussion about the complexity of realloc and suggestions on how to tame that complexity.

In all, it's a very thorough and impressive discussion of just 8 lines of code — and Cody's other answers are equally top-notch.

200_success Mod
  • 145.7k
  • 4
  • 114
  • 284