What happens with old allocated memory when realloc fails?

I

insomninja

Consider the following:


int* pi = malloc(sizeof(int));
//-- stuff goes here --
pi = realloc(2*sizeof(int));


If realloc fails it will return a null pointer but will it also free
the memory that pi previously pointed to?
Or do I have to realloc to a temporary variable so I still have a
pointer to the original data?
Like this:


int* pi = malloc(sizeof(int));
//-- stuff goes here --
int* tmp = realloc(2*sizeof(int));
if (tmp != 0) {
pi=tmp;
}
else {
// realloc failed, but I still have the memory previously allocated
at *pi so I can free it if I need to.
}
 
K

Keith Thompson

Consider the following:

int* pi = malloc(sizeof(int));
//-- stuff goes here --
pi = realloc(2*sizeof(int));

You mean
pi = realloc(pi, 2*sizeof(int));
or, better:
pi = realloc(pi, 2 * sizeof *pi);
If realloc fails it will return a null pointer but will it also free
the memory that pi previously pointed to?
Or do I have to realloc to a temporary variable so I still have a
pointer to the original data?

realloc() is explained in question 7.29 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.

It should also be explained in any decent C textbook, or by the online
documentation of any decent C implementation.

People spend a lot of time and effort writing documentation. When you
don't use it, it makes them sad.
 
J

jmcgill

Consider the following:


int* pi = malloc(sizeof(int));
//-- stuff goes here --
pi = realloc(2*sizeof(int));

If realloc fails it will return a null pointer but will it also free
the memory that pi previously pointed to?

realoc() will not free the memory that pi referred to, but assigning the
NULL return from realloc() will clobber pi, leaving you unable to free it.


Also, realloc is

void *realloc(void *ptr, size_t size);

but you know that.

You definitely want to use some other variable for the return value of
realloc(), because as you correctly observe, clobbering your int* pi
with NULL will lead to a leak.
 
I

insomninja

I found the answer in FAQ Q/A 7.29. I did do some google searches
before though, but they didn't turn up anything.

Thanks for the help.
 
K

Kenneth Brody

I found the answer in FAQ Q/A 7.29. I did do some google searches
before though, but they didn't turn up anything.

You can always try Googling for something like:

man page realloc

Or, for a particular platform, something like:

unix man page realloc

Note, however, that these may include system-specific answers. To
narrow it down to the clc FAQ, try something like:

clc realloc site:faqs.org

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top