J
James S. Singleton
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?
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?