Re: How does free() know how many bytes to free???



MAx wrote:

Hi,
A call to free() is used to de allocate memory which has
been
allocated by a malloc()
or a calloc() call.
How does free() know how many bytes to deallocate?

Upon malloc, the size of the buffer is stored somewhere, normally
sizeof(ptrsize_t) bytes before the beginning of the allocated
area, but this may largely differ by the implementation. A
ptr->buffer_length lookup table may be used as well.

Have a look at the source code of your libc.

Wolfgang Draxinger
--
E-Mail address works, Jabber: hexarith@xxxxxxxxxx, ICQ: 134682867

.



Relevant Pages

  • Re: Simple question about headers and malloc!
    ... that I am getting junk when reading the first memory allocated by malloc. ... You allocate memory, and you hang onto the pointer (not ... struct MCB{ ...
    (microsoft.public.vc.language)
  • Re: Are the following 2 code statements equivalent
    ... In the calloc() case, the ... is not allowed to allocate some smaller amount. ... You call memsetwithout checking the result of the malloc() call. ... If mallocfails, it returns a null pointer, and the call to memset ...
    (comp.lang.c)
  • Re: xmalloc string functions
    ... Failed malloc() is. ... Why can't you test an allocation failure? ... each can fail on request, each needs to have the request failure dealt ... process Save click because it failed to allocate memory for the event ...
    (comp.lang.c)
  • Re: Dynamic C String Problem
    ... I read that calloc will initialize the size to zero in contradiction to ... malloc; i honeslty learned C once and through implementation didn't get ... it be allocating 128 Bytes a waste to allocate such unnecessary memory ... the bookkeeping required to allocate memory. ...
    (comp.lang.c)
  • Re: using malloc and calloc - help
    ... > in understanding malloc and calloc use in C. ... allocate memory for a block of these structures? ... the allocated space is usually filled with specific data, ...
    (alt.comp.lang.learn.c-cpp)