473,322 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

What happens with old allocated memory when realloc fails?

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.
}

Aug 8 '06 #1
4 3847
in********@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 8 '06 #2
in********@gmail.com wrote:
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.

Aug 8 '06 #3
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.

Aug 8 '06 #4
in********@gmail.com wrote:
>
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:Th*************@gmail.com>
Aug 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Kumar | last post by:
Hi, Can somebody advise me on what is the best alternative for realloc in C++? Is it safe to "realloc"ate memory allocated using new operator ?
7
by: serikas | last post by:
Is there a way to get aligned dynamically allocated memory? (provided that the requested memory size is a power of 2.) For example, if I request 128 bytes of memory, can I implement an allocator...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
11
by: binaya | last post by:
Dear all, Say I allocate a block of memory using the command malloc.I have a question. Can I deallocate certain portion of it only during runtime ? For eg: Say I allocate 5 pointers to int...
17
by: Radith | last post by:
In follow-up to my previous post; Would any one know how I can intake sentences of text (i.e. With space characters); because scanf("%s") causes input to terminate after a space character. ...
19
by: Jerry | last post by:
Hi,all I write a function to copy one memory buffer to anther with dynamic allocationg when the destination is not large enough. The code is below: void DSM_CC::fillBuf(unsigned char**...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
82
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am...
8
Hackles
by: Hackles | last post by:
Hello, Is it possible to retrieve the size of allocated memory from a pointer (more specifically, a pointer returned my malloc/realloc). This question may have already been asked countless times....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.