Think about what realloc
does. How can a free
pointer to address x+1
when malloc
actually create a pointer to address x
?
In more specific terms, suppose you allocate 100 bytes at 0x1000. Now x
incremented, indicating 0x1001. Then you call realloc
at the new address. Since none of the malloc
, calloc
and realloc
created 0x1001, the free
(or equivalent code) used by the realloc
call has no idea how to do anything with 0x1001; he cannot even understand how many bytes of memory he is occupying. He knows only about 100 bytes in 0x1000.
The basic idea of โโimplementing malloc
and friends is that you keep track of the assigned pointers and the number of bytes allocated. Then, when free
is called later, a pointer to free
displayed. If there is no link to this pointer passed to free
, what else needs to be done except for the failure? This is more logical for me than assuming that you can continue to use a pointer, which may or may not be valid.
source share