Re: More questions on realloc - Thanks




James S. Singleton wrote:
> Thanks everybody for your replies. I gather that:
> b) Implementing realloc(ptr, size) using malloc(), memcpy() and free()
> alone, without knowing the size of the memory region pointed to by ptr, is
> just not possible.

Sure it is possible. Just inefficient. You can malloc a new
space, free the old (if malloc succeeds), and copy on all calls
to realloc. What you lose is the ability to reuse the existing space
in cases where the new size is <= the old, or in cases where there
is additional free space after the current block. Which is likely a
less than optimal way to do things, but could be good enough.

-David

.



Relevant Pages

  • Re: More questions on realloc - Thanks
    ... >>> alone, without knowing the size of the memory region pointed to by ptr, is ... >> space, free the old (if malloc succeeds), and copy on all calls ... >> is additional free space after the current block. ...
    (comp.lang.c)
  • Re: [Q] about free(ptr)
    ... when malloc succeeds. ... (And "ptr" is extraneous.) ... allocations can be a useful technique for avoiding a significant ... for programs that can tolerate the performance ...
    (comp.lang.c)
  • Re: how to calculate "free space propagation constant"
    ... Given wavelength of a laser, how to calculate its free space ... How can you be active in optics without knowing and understanding this most ...
    (sci.optics)