Re: More questions on realloc





James S. Singleton wrote On 11/29/05 12:01,:
> Thanks to everybody who provided an answer to my previous question on
> this. I am trying to grasp the nitty-gritty details of this, and I am
> having difficulties understanding some of the issues involved.
>
> According to the man page (and I quote)
>
> realloc(void *ptr, size_t size) changes the size of the memory block
> pointed to by ptr to size bytes. The contents will be unchanged to the
> minimum of the old and new sizes; newly allocated memory will be
> uninitialized.
>
> Let's assume the following sequence (my apologies for the sloppiness of
> my language in what follows; I am taking for granted that experienced C
> insiders will have no difficulty cutting through it):
>
> p = malloc(size1) ;
>
> /* Code to copy some stuff to the memory area pointed to by p */
>
> q = realloc(p, size2) ;
>
> For simplicity, I will assume that q != p. With this, my understanding is
> that realloc will copy the contents of p to q, to the appropriate length,
> and will then free p.
>
> My question is, how does realloc know that p points to a memory region
> size1 bytes in length? Or, to put it differently, can realloc be
> implemented with malloc, and no other memory management call?

The memory-management functions keep track of the
sizes of allocated and available blocks of memory. The
details of how this is done differ from one implementation
to another. One frequently-used method is to allocate a
little bit more memory than is requested, plant some
housekeeping data at the beginnning, and return a pointer
to the tail end. Later, free() or realloc() can look "just
before" the pointer they're given to find the housekeeping
data again.

... but that's just one way; others exist. From the
programmer's perspective, it's all "implementation magic,"
just like the machinery behind, say, fopen(). You don't
(usually) care how the spell is cast, just that the magic
happens.

--
Eric.Sosman@xxxxxxx

.



Relevant Pages

  • Re: REALLOC AND FREE
    ... If i allocate some memory using mallocand then ... Given a pointer to the first byte in a block of memory successfully ... If realloc returns a null ...
    (comp.lang.c)
  • Re: Realloc
    ... can allocate an arbitrary amount of memory. ... there is no more limit than there is for the malloc() case. ... Depending on this, could realloc ever ...
    (comp.lang.c)
  • Re: Realloc
    ... then I can allocate an arbitrary amount of memory. ... I call realloc on an already existing block of memory (to increase ... different limit that that for malloc. ...
    (comp.lang.c)
  • Re: memory allocation questions (newbie)
    ... > memory leaks. ... If you allocate some memory (because the required ... epically each pointer in it to have all pointer ... sometimes it is more easy to use realloc than malloc+ copy ...
    (comp.lang.c)
  • Re: Is there a maximum contiguous memory allocation?
    ... but could ALLOCATE it! ... allocate it if I had 2GB of physical memory! ... the amount of physical memory I have installed. ... Note that you can use raw VirtualAlloc to improve your ...
    (microsoft.public.vc.mfc)