Re: More questions on realloc - Thanks



David Resnick wrote:
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.

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.

If you know the size of the region, then obviously copying works. But the OP specifically said we don't.

S.
.



Relevant Pages

  • Re: virtual memory question
    ... ptr = malloc; ... while (ptr) ... Don't confuse address space with actual memory usage or availability. ... Furthermore of the 4GB address space that is available to each process the upper half is reserved for the kernel and is the same for all the processes running. ...
    (comp.os.linux.misc)
  • Thank You -- Thomas J. Gritzan
    ... Thomas -- Your suggestion to malloc() out a block of memory was the ... Below are some details of my memory issues ... ... As a work around solution I guessed a ram disk would solve the ... persistence will frustrate the off topic police and give them a target ...
    (comp.lang.c)
  • Re: Simple question about headers and malloc!
    ... Therefore I am making all of its declarations ... memory (using malloc) and then exit back to main. ... allocation, I get data strored from the second allocation... ...
    (microsoft.public.vc.language)
  • Re: ten thousand small processes
    ... Stack needs to be executable for the current signal trampoline ... the use of malloc() that is causing your primary ... if there is any heap memory in use at all, no matter what you do, ... either directly, as a 4M page mapping (not used for user processes, ...
    (freebsd-performance)
  • Re: Help with Enter and Leave Instructions
    ... >>> for Memory Accesses, ... > The only standard way to do it is via malloc. ... Uh, SBRK/BRK is a standard, documented system call. ... > I really don't understand the objection to using C libraries. ...
    (alt.lang.asm)