By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Can realloc potentially cause a memory leak?

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Nov 13 '05 #2

P: n/a
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.

Nov 13 '05 #3

P: n/a
Mac
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
--

Nov 13 '05 #4

P: n/a
Jeff Rodriguez <ne********@gurugeek.EXAMPLENOSPAM.com> 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.
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.