malloc, realloc, free questions
Author |
Message |
Martin F #1 / 35
|
 malloc, realloc, free questions
Hi. I have some questions about C's memory allocation functions. Let's assume buf is a pointer to storage allocated with malloc, and n is a positive integer. What do these funtion calls do and return? malloc(0) realloc(buf, 0) realloc(0, n) realloc(0, 0) free(0) And some more questions: If I call realloc(buf, n) and it fails, is the old buffer lost? Does realloc(buf, n) always succeed if n is less than the old size of the buffer? --
|
Mon, 18 Oct 2004 05:28:49 GMT |
|
 |
Dave Hans #2 / 35
|
 malloc, realloc, free questions
Quote: >Hi. I have some questions about C's memory allocation functions. >Let's assume buf is a pointer to storage allocated with malloc, and >n is a positive integer. What do these funtion calls do and return? >malloc(0)
Implementation-defined. Either a null pointer or a unique pointer. Quote: >realloc(buf, 0)
Assuming buf points to a previously-allocated and never-freed buffer, The buffer is freed. Otherwise, if buf is not a null pointer, the behavior is undefined. I believe it must return a null pointer, but I'm not certain. Quote: >realloc(0, n)
Behaves as malloc(n) Quote: >realloc(0, 0)
Behaves as malloc(0) Quote: >free(0)
No action occurs. Quote: >And some more questions: >If I call realloc(buf, n) and it fails, is the old buffer lost?
No. Quote: >Does realloc(buf, n) always succeed if n is less than the old size >of the buffer?
There's no guarantee. However, if it does fail, the old buffer remains. Regards, -=Dave -- Change is inevitable, progress is not. --
|
Tue, 19 Oct 2004 05:18:42 GMT |
|
 |
Barry Schwar #3 / 35
|
 malloc, realloc, free questions
Quote: >Hi. I have some questions about C's memory allocation functions. >Let's assume buf is a pointer to storage allocated with malloc, and >n is a positive integer. What do these funtion calls do and return? >malloc(0)
Implementation defined. May return NULL or a value. If a value, the value may not be dereferenced. Quote: >realloc(buf, 0)
The area pointed to by buff is deallocated (freed). Return value same as malloc(0) above. Quote: >realloc(0, n)
Identical to malloc(n). Quote: >realloc(0, 0)
Identical to malloc(0) above. Quote: >free(0) Does nothing. >And some more questions: >If I call realloc(buf, n) and it fails, is the old buffer lost? No. >Does realloc(buf, n) always succeed if n is less than the old size >of the buffer?
Not guaranteed. I wonder if there are any implementations where it could fail. <<Remove the del for email>> --
|
Tue, 19 Oct 2004 05:18:54 GMT |
|
 |
CBFalcone #4 / 35
|
 malloc, realloc, free questions
Quote:
> Hi. I have some questions about C's memory allocation functions. > Let's assume buf is a pointer to storage allocated with malloc, and > n is a positive integer. What do these funtion calls do and return? > malloc(0) > realloc(buf, 0) > realloc(0, n) > realloc(0, 0) > free(0)
realloc(NULL, n) is equivalent to malloc(n). free(NULL) is a run-time error, or at least undefined behaviour. The others are implementation defined. (0 is equivalent to NULL in C when used as a pointer) Quote: > And some more questions: > If I call realloc(buf, n) and it fails, is the old buffer lost?
If you overwrite the contents of the pointer 'buf', yes, otherwise no. So you never write "buf = realloc(buf, whatever);" but use: if (temp = realloc(buf, whatever)) buf = temp; else { /* miserable failure, buf unharmed */ } Quote: > Does realloc(buf, n) always succeed if n is less than the old size > of the buffer?
Not necessarily. My proposed version for the DJGPP system, which is quite portable, can be seen at: <http://cbfalconer.home.att.net/download/nmalloc.zip> All the GCC dependant macros can be redefined to nothing via NDEBUG. Unfortunately the calls remain non-standard for non GCC compilers, but can be simply commented out. Until C99 varargs macros are available that will remain. --
Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address! --
|
Tue, 19 Oct 2004 05:18:56 GMT |
|
 |
Richard B #5 / 35
|
 malloc, realloc, free questions
Quote:
> Hi. I have some questions about C's memory allocation functions. > Let's assume buf is a pointer to storage allocated with malloc, and > n is a positive integer. What do these funtion calls do and return?
Any complete textbook on C should tell you this, but... Quote: > malloc(0) > realloc(buf, 0)
Either null, or a (useless) pointer to zero bytes of memory. Quote: > realloc(0, n)
Same thing as malloc(n). Quote: > realloc(0, 0)
Same thing as malloc(0)... Quote: > free(0)
Nothing. Quote: > And some more questions: > If I call realloc(buf, n) and it fails, is the old buffer lost?
No. The buffer still exists at the old address, but realloc() returns a null pointer. That is why this: ptr=realloc(ptr, newsize); is a bad idea; if realloc() fails, your old address is lost, and you've leaked that memory. Quote: > Does realloc(buf, n) always succeed if n is less than the old size > of the buffer?
That's a quality of implementation issue, so the Standard doesn't (AFAICT) address it, but I'd be disappointed in an implementation that didn't. Richard --
|
Tue, 19 Oct 2004 05:19:03 GMT |
|
 |
Hans-Bernhard Broeke #6 / 35
|
 malloc, realloc, free questions
Quote:
> malloc(0)
Implementation-defined. Quote: > realloc(buf, 0)
Implementation-defined. Quote: > realloc(0, n)
Equivalent to malloc(n). Quote: > realloc(0, 0)
Implementation-defined. In all of the above, the result is still required to be either a null pointer or pointer to allocated memory, i.e. the implementation isn't at liberty to return garbage or kill your program, just like that Quote: > free(0)
Nothing happens. No value returned. Quote: > If I call realloc(buf, n) and it fails, is the old buffer lost?
No. That's why you should never buf = realloc(buf, n); but rather something like this: { void * temp = realloc(buf,n); if (temp) buf = temp; else { /* error, optionally tell user about it */ } } Quote: > Does realloc(buf, n) always succeed if n is less than the old size > of the buffer?
Usually it will. But don't bet your home on that --- it's not strictly guaranteed, so there may well be some extra-peculiar platform where it can fail. Either check it didn't fail, or avoid calling realloc() for downsizing. --
Even if all the snow were burnt, ashes would remain. --
|
Tue, 19 Oct 2004 05:19:08 GMT |
|
 |
hope #7 / 35
|
 malloc, realloc, free questions
| Hi. I have some questions about C's memory allocation functions. | Let's assume buf is a pointer to storage allocated with malloc, and | n is a positive integer. What do these funtion calls do and return? | | malloc(0) The behavior is implementation-defined: the value returned is either a null pointer or a unique pointer. | realloc(buf, 0) If buf is a non-null pointer whose value is returned by a previous call to calloc, malloc or realloc and has not been deallocated by a subsequent call to free or realloc: the object pointed to by buf is deallocated. If buf is a null-pointer the behavior is equivalent to malloc(0). Either a null pointer or a unique pointer that can be successfully passed to free is returned. | realloc(0, n) Equivalent to malloc(n). | realloc(0, 0) Equivalent to malloc(0) | free(0) No action. | | And some more questions: | If I call realloc(buf, n) and it fails, is the old buffer lost? No. Upon success: the object remains unchanged up to the lesser of the new and old sizes. Upon failure: the object remains unchanged. | Does realloc(buf, n) always succeed if n is less than the old size | of the buffer? No. --
|
Tue, 19 Oct 2004 05:19:23 GMT |
|
 |
Tim Goodwi #8 / 35
|
 malloc, realloc, free questions
Quote:
> Let's assume buf is a pointer to storage allocated with malloc, and > n is a positive integer. What do these funtion calls do and return? > malloc(0)
`malloc(0)' is allowed to return either a null pointer, or a non-null pointer that does not point to an object. (There has been debate in other newsgroups over whether multiple calls to `malloc(0)' must return different non-null pointers, but since the implementation is always allowed to return null instead, I don't think it makes any practical difference.) Quote: > realloc(buf, 0)
Equivalent to `free(buf)'. Quote: > realloc(0, n)
Equivalent to `malloc(n)'. Quote: > realloc(0, 0)
Equivalent to `malloc(0)'. See above. Quote: > free(0)
Does nothing. Quote: > If I call realloc(buf, n) and it fails, is the old buffer lost?
No, but a common mistake is to write code that loses the only pointer to the buffer, like this. buf = realloc(buf, n); if (!buf) { ... deal with out of memory condition, but we no longer have a pointer to the buffer If "dealing" with the lack of memory just means exit()ing, this is no problem, but if you need to clear up, you have to write code like this. newbuf = realloc(buf, n); if (newbuf) buf = newbuf; else { ... deal with out of memory condition: buf is still valid Quote: > Does realloc(buf, n) always succeed if n is less than the old size > of the buffer?
No. There are plausible memory allocation strategies which can't make this guarantee, so it is not required by the standard. Tim. -- Tim Goodwin | "But fixing the entire UNIX system is not Leicester, UK | on my TODO list. (Yet.)" -- Dan Bernstein --
|
Tue, 19 Oct 2004 05:19:26 GMT |
|
 |
kgol #9 / 35
|
 malloc, realloc, free questions
Nice quiz. Let's see how I do. Quote:
> Hi. I have some questions about C's memory allocation functions. > Let's assume buf is a pointer to storage allocated with malloc, and > n is a positive integer. What do these funtion calls do and return? > malloc(0)
Returns something implementation dependent, sometimes null, but it won't crash the program. Quote: > realloc(buf, 0)
Same as free(buf) Quote: > realloc(0, n)
Same as malloc(n). Common idiom. Quote: > realloc(0, 0)
Same as malloc(0) Quote: > free(0)
Does nothing. Common idiom. Quote: > If I call realloc(buf, n) and it fails, is the old buffer lost?
The contents of memory are unchanged. But if you do buf = realloc(buf,n) and you haven't saved a copy of the buf pointer somewhere, you lose the pointer. I do something like tmp = realloc(buf,n); if (tmp != null) { buf = tmp; } Quote: > Does realloc(buf, n) always succeed if n is less than the old size > of the buffer?
Stumped. My intuition would say yes, but I wonder if the standard guarantees it. I've never needed to realloc to a smaller value. --
--
|
Tue, 19 Oct 2004 05:19:28 GMT |
|
 |
those who know me have no need of my nam #10 / 35
|
 malloc, realloc, free questions
Quote: >Let's assume buf is a pointer to storage allocated with malloc, and >n is a positive integer. What do these funtion calls do and return? >malloc(0) >realloc(buf, 0)
these are quality of implementation issues. NULL would be understandable, but most probably return a pointer that you can/dare/should not dereference. in the later case memory is probably allocated (hence removed from the free list/pool/space/&c), though as with other allocation requests you shouldn't expect any such slop to actually be available for use. Quote: >realloc(0, n) >realloc(0, 0)
as if malloc were called, i.e., malloc(n) and malloc(0) respectively. Quote: >free(0)
nothing. Quote: >If I call realloc(buf, n) and it fails, is the old buffer lost?
no, as long as you don't hose buf by doing ``buf=realloc(buf,n)''. Quote: >Does realloc(buf, n) always succeed if n is less than the old size >of the buffer?
it's likely to succeed, but there is no guarantee. be defensive, expect to handle failures. -- bringing you boring signatures for 17 years --
|
Tue, 19 Oct 2004 05:19:30 GMT |
|
 |
Andy Isaacs #11 / 35
|
 malloc, realloc, free questions
Quote:
>Hi. I have some questions about C's memory allocation functions.
[snip malloc(0), realloc(0), etc] These sound like homework questions. The answers are available to a cursory google search, and even a quick glance at either K&RII or the standard will tell you the answers. You should do your own homework, or else explain why this is not a pathetic plea for someone else to do your homework for you. -andy --
|
Tue, 19 Oct 2004 05:19:43 GMT |
|
 |
Rob #12 / 35
|
 malloc, realloc, free questions
Quote: > Hi. I have some questions about C's memory allocation functions. > Let's assume buf is a pointer to storage allocated with malloc, and > n is a positive integer. What do these funtion calls do and return? > malloc(0)
Return is implementation dependent, but you should never dereference it. i.e. treat it like a NULL pointer, even though it's value may be different from NULL. Quote: > realloc(buf, 0)
Assuming buf was allocated by malloc/calloc/realloc, it is deallocated. The returned value is (like malloc (0)) implementation dependent, and should never be dereferenced. Quote: > realloc(0, n)
Like malloc(n). Quote: > realloc(0, 0)
Special case of the previous value: like malloc(0) Quote: > free(0)
No effect. Quote: > And some more questions: > If I call realloc(buf, n) and it fails, is the old buffer lost?
No. That is actually the purpose of realloc. Quote: > Does realloc(buf, n) always succeed if n is less than the old size > of the buffer?
Not formally guaranteed. In practice, it will often succeed, but if you genuinely care about portability and maintenance you need to allow for the possibility it won't. --
|
Wed, 20 Oct 2004 06:32:57 GMT |
|
 |
Peter Burk #13 / 35
|
 malloc, realloc, free questions
Quote: > Hi. I have some questions about C's memory allocation functions. > Let's assume buf is a pointer to storage allocated with malloc, and > n is a positive integer. What do these funtion calls do and return?
So to summarize: Quote: > malloc(0)
Either a null pointer or a unique pointer - or - Either a null pointer or some other pointer Quote: > realloc(buf, 0)
Same as free(buf), returning null - or - Same as free(buf), returning some pointer Quote: > realloc(0, n)
Same as malloc(n) (Everybody agrees about this one!) Quote: > realloc(0, 0)
Same as malloc(0) (Everybody agrees about this one, too) Quote: > free(0)
Does nothing - or - Crashes your program /peter --
|
Wed, 20 Oct 2004 06:33:00 GMT |
|
 |
Tony Finc #14 / 35
|
 malloc, realloc, free questions
Quote:
>realloc(NULL, n) is equivalent to malloc(n). free(NULL) is a >run-time error, or at least undefined behaviour.
Good grief, I would expect you to know that free(NULL) is guaranteed to be a no-op! Tony. --
BAILEY: VARIABLE BECOMING SOUTHEASTERLY 3 OR 4, INCREASING 5 OR 6 IN WEST LATER. SHOWERS. GOOD. --
|
Wed, 20 Oct 2004 06:33:02 GMT |
|
 |
Tony Finc #15 / 35
|
 malloc, realloc, free questions
Quote:
>>realloc(buf, 0) >Assuming buf points to a previously-allocated and never-freed buffer, >The buffer is freed. Otherwise, if buf is not a null pointer, the >behavior is undefined.
No, it's the same as malloc(0), i.e. implementation-defined. Tony. --
DOVER WIGHT: WEST OR NORTHWEST VEERING NORTHEAST 3 OR 4, OCCASIONALLY 5 LATER. THUNDERY SHOWERS DYING OUT. MODERATE OR GOOD. --
|
Wed, 20 Oct 2004 06:33:04 GMT |
|
|
Page 1 of 3
|
[ 35 post ] |
|
Go to page:
[1]
[2] [3] |
|