Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > More questions on realloc

Reply
Thread Tools

More questions on realloc

 
 
James S. Singleton
Guest
Posts: n/a
 
      11-29-2005
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?








 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      11-29-2005
On Tue, 29 Nov 2005 17:01:17 GMT, in comp.lang.c , "James S.
Singleton" <(E-Mail Removed)> wrote:

> My question is, how does realloc know that p points to a memory region
>size1 bytes in length?


By magic.

Seriously, this is an implementation detail that the end user doesn't
actually need to know. Under the hood it might be doing all sorts of
things (a lookup table of malloced blocks and their sizes, storing the
size in memor just before the malloced block, etc etc. But you don't
need to know - it just works.

>Or, to put it differently, can realloc be
>implemented with malloc, and no other memory management call?


You'd need memcpy and free. How else can you copy the memory from the
old to the new pointers, and then free the old one?

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-29-2005


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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Robert Harris
Guest
Posts: n/a
 
      11-29-2005
Mark McIntyre wrote:
> On Tue, 29 Nov 2005 17:01:17 GMT, in comp.lang.c , "James S.
> Singleton" <(E-Mail Removed)> wrote:
>
>
>> My question is, how does realloc know that p points to a memory region
>>size1 bytes in length?

>
>
> [snip]
>
>>Or, to put it differently, can realloc be
>>implemented with malloc, and no other memory management call?

>
>
> You'd need memcpy and free. How else can you copy the memory from the
> old to the new pointers, and then free the old one?
>

But realloc doesn't always have to reallocate memory; if you malloc,
say, 10 bytes, you may well actually get 32 (or 1024) bytes. Your C
library knows how much but you don't, so calling realloc() is often a
win over malloc(), memcpy() and free().

Robert
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      11-29-2005
On Tue, 29 Nov 2005 17:27:48 GMT, in comp.lang.c , Robert Harris
<(E-Mail Removed)> wrote:

>Mark McIntyre wrote:
>> On Tue, 29 Nov 2005 17:01:17 GMT, in comp.lang.c , "James S.
>> Singleton" <(E-Mail Removed)> wrote:
>>
>>>Or, to put it differently, can realloc be
>>>implemented with malloc, and no other memory management call?

>>
>> You'd need memcpy and free. How else can you copy the memory from the
>> old to the new pointers, and then free the old one?
>>

>But realloc doesn't always have to reallocate memory; if you malloc,
>say, 10 bytes, you may well actually get 32 (or 1024) bytes.


This is sometimes true, but not relevant to the OP since
a) you don't know if your compiler can do this and
b) you don't know if it did, in any specific case and
c) it may in any events not be able to, in any specific case.

Whatever, realloc can under the hood operate however it likes,
provided it behaves *as if* it had done a new *alloc, copy and free.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Niklas Norrthon
Guest
Posts: n/a
 
      11-30-2005
"James S. Singleton" <(E-Mail Removed)> writes:

> 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?


As others already have said, it's an implementation detail. My guess is that the
most common scheme is to have the size of a memory block stored immediatly before
the block in question. This is also the reason why writing beyond the end of an
allocated block can have so strange effects, crashing a program much later in
a totally different context, making it difficult to debug.

K & R has a section describing a typical memory allocator. I recommend you to
read that.

Realloc could be implemented in terms of malloc, free, and memcpy, but you
would lose the possibility to extend an existing memory block without the
need of copy in the case that there are enough free memory adjacent to the
current block. Note that it is trivial to implement both malloc and free in
terms of realloc:

void* malloc(size_t sz) { return realloc(NULL, sz); }
void free(void* ptr) { realloc(ptr, 0); }

/Niklas Norrthon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
More questions on realloc - Thanks James S. Singleton C Programming 10 12-01-2005 04:37 PM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
JNI:Solaris 9-Signal 11 in realloc() Mr T Java 0 11-19-2003 10:22 AM



Advertisments