Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Can realloc potentially cause a memory leak?

Reply
Thread Tools

Can realloc potentially cause a memory leak?

 
 
Jeff Rodriguez
Guest
Posts: n/a
 
      11-19-2003
If you realloc, and the new memory has a different address, what happens at the
old address? Is that memory basically free()'d, or do you need to keep track of
address changes and manually free() ?

Jeff

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      11-19-2003
Jeff Rodriguez wrote:
> If you realloc, and the new memory has a different address, what happens
> at the old address? Is that memory basically free()'d, or do you need to
> keep track of address changes and manually free() ?
>

It is free()-ed.

HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

 
Reply With Quote
 
 
 
 
Morris Dovey
Guest
Posts: n/a
 
      11-19-2003
Jeff Rodriguez wrote:

> If you realloc, and the new memory has a different address, what happens
> at the old address? Is that memory basically free()'d, or do you need to
> keep track of address changes and manually free() ?


Jeff...

It doesn't cause a memory leak. It works to consider that memory
freed for re-use; but the actual (under the covers) disposition
is up to the implementation.

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

 
Reply With Quote
 
Mac
Guest
Posts: n/a
 
      11-19-2003
On Tue, 18 Nov 2003 19:02:07 +0000, Jeff Rodriguez wrote:

> If you realloc, and the new memory has a different address, what happens at the
> old address? Is that memory basically free()'d, or do you need to keep track of
> address changes and manually free() ?
>
> Jeff


In the case you seem to be discussing, the memory is effectively freed,
and you MUST not free it.

Misuse of realloc CAN cause a memory leak, but only when allocation fails.

Here's an example:


#include <stdlib.h>
....
char *foo;
int buf_size = 1;
....

foo = malloc(buf_size);
....

foo = realloc(foo, buf_size *= 2);

Here, if realloc fails, the memory previously pointed to by foo will still
be claimed, but you will have lost your only pointer to it, because
realloc returns NULL on failure. This is a memory leak.

HTH

Mac
--

 
Reply With Quote
 
Derk Gwen
Guest
Posts: n/a
 
      11-19-2003
Jeff Rodriguez <(E-Mail Removed)> wrote:
# If you realloc, and the new memory has a different address, what happens at the
# old address? Is that memory basically free()'d, or do you need to keep track of
# address changes and manually free() ?

If the returned address is different from the input address, the input
address is no longer valid, as if it were passed to free; it can no longer
be safely freed, reallocked, or used to access memory. What actually happens
is implementation dependent.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
This is one wacky game show.
 
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
Can realloc(p,0) return NULL when p is non-NULL and memory aplenty? Francois Grieu C Programming 14 03-07-2008 05:40 PM
New site. Can anyone tell me if I am doing something potentially bad BEFORE I commit? Mike Barnard HTML 27 02-18-2008 02:00 AM
will all these messages cause a problem . I am a new subscriber and my computer is downloading 100,000 messages. Will this cause any kind of a problem with my ability to store other items?? Camille White Camille White Computer Support 9 11-08-2004 01:13 AM
Does standard C guarantee same memory content after realloc()? Jonas C Programming 21 10-13-2003 11:15 PM
[Q]function doesnot declare 'throws...' even though it can potentially throw an exception... Paul Java 1 08-14-2003 06:37 PM



Advertisments