Re: More questions on realloc - Thanks
- From: Skarmander <invalid@xxxxxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 20:04:24 +0100
David Resnick wrote:
James S. Singleton wrote:How do you copy memory if you don't know the size of the old region? There is no way to implement realloc(ptr, size) as a separate function, without access to the internal memory management structures. If you think otherwise, please supply the code. I have a feeling you're talking about something else entirely, but it's unclear what.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.
If you know the size of the region, then obviously copying works. But the OP specifically said we don't.
S. .
- Follow-Ups:
- Re: More questions on realloc - Thanks
- From: David Resnick
- Re: More questions on realloc - Thanks
- References:
- More questions on realloc - Thanks
- From: James S. Singleton
- Re: More questions on realloc - Thanks
- From: David Resnick
- More questions on realloc - Thanks
- Prev by Date: PayPal Opportunity
- Next by Date: Re: direct string manuplation not working
- Previous by thread: Re: More questions on realloc - Thanks
- Next by thread: Re: More questions on realloc - Thanks
- Index(es):
Relevant Pages
|