473,657 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

when can realloc fail?

Hi,
I just want to get this right.
A call to realloc() will return NULL on error and the original memory
is left untouched,
both when requesting a larger or a smaller size that the original,
right?
But a call to realloc() with size set to zero is equivalent to free(),
with returns void.
Does that mean that a call to realloc() can fail when shinking memory
except when
shrinking it to zero in which case it will always succeed?

Thanks!

Apr 1 '07 #1
31 12752
ba******@hushma il.com wrote:
Hi,
I just want to get this right.
A call to realloc() will return NULL on error and the original memory
is left untouched,
both when requesting a larger or a smaller size that the original,
right?
But a call to realloc() with size set to zero is equivalent to free(),
with returns void.
Does that mean that a call to realloc() can fail when shinking memory
except when
shrinking it to zero in which case it will always succeed?
Yes, realloc() can fail when shrinking an allocation, or
when growing it, or even when leaving its size unchanged.

When reallocating to zero size, the situation is complicated.
C90 says "If size is zero and ptr is not a null pointer, the
object it points to is freed." Since free() cannot fail (given
an argument that doesn't invoke undefined behavior), it follows
that realloc(...,0) cannot fail.

But C99 has no such text, and makes no special case for size
zero. All we're told is that realloc(...,0) either fails or it
returns a pointer to an object of size zero. Nothing I can find
in C99 forbids realloc(...,0) to fail, so presumably it can.

--
Eric Sosman
es*****@acm-dot-org.invalid
Apr 1 '07 #2
ba******@hushma il.com wrote:
Hi,
I just want to get this right.
A call to realloc() will return NULL on error
The word failure might be more appropriate in this context than the
word error.
and the original memory is left untouched,
both when requesting a larger or a smaller size that the original, right?
Yes.
But a call to realloc() with size set to zero is equivalent to free(),
Semantically yes.
with returns void.
No, it returns an indeterminate value.
Does that mean that a call to realloc() can fail when shinking memory except when
shrinking it to zero in which case it will always succeed?
No. realloc is not guaranteed to suceed even when the new size is zero.

Apr 1 '07 #3
On Apr 1, 9:06 pm, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
banan...@hushma il.com wrote:
Hi,
I just want to get this right.
A call to realloc() will return NULL on error and the original memory
is left untouched,
both when requesting a larger or a smaller size that the original,
right?
But a call to realloc() with size set to zero is equivalent to free(),
with returns void.
Does that mean that a call to realloc() can fail when shinking memory
except when
shrinking it to zero in which case it will always succeed?

Yes, realloc() can fail when shrinking an allocation, or
when growing it, or even when leaving its size unchanged.

When reallocating to zero size, the situation is complicated.
C90 says "If size is zero and ptr is not a null pointer, the
object it points to is freed." Since free() cannot fail (given
an argument that doesn't invoke undefined behavior), it follows
that realloc(...,0) cannot fail.

But C99 has no such text, and makes no special case for size
zero. All we're told is that realloc(...,0) either fails or it
returns a pointer to an object of size zero. Nothing I can find
in C99 forbids realloc(...,0) to fail, so presumably it can.
Thank you for your reply!
But I wonder, if realloc(...,0) fails in C99, how can I know that?
How can I know if a returned NULL means that realloc() failed,
or if it is the pointer to the new memory, empty, which is NULL.
(That is what I got on my compiler anyway)

Apr 1 '07 #4
On Apr 1, 9:07 pm, "santosh" <santosh....@gm ail.comwrote:
banan...@hushma il.com wrote:
Hi,
I just want to get this right.
A call to realloc() will return NULL on error

The word failure might be more appropriate in this context than the
word error.
and the original memory is left untouched,
both when requesting a larger or a smaller size that the original, right?

Yes.
But a call to realloc() with size set to zero is equivalent to free(),

Semantically yes.
with returns void.

No, it returns an indeterminate value.
What? My manpage says "void free(void *ptr);",
doesn't free return void? By that I meant, free doesn't return
anything.
Does that mean that a call to realloc() can fail when shinking memory except when
shrinking it to zero in which case it will always succeed?

No. realloc is not guaranteed to suceed even when the new size is zero.
And as I wrote in the other post, I wonder how I can know that.

Apr 1 '07 #5
<ba******@hushm ail.comha scritto nel messaggio
news:11******** *************@y 66g2000hsf.goog legroups.com...
Does that mean that a call to realloc() can fail when shinking memory
except when
shrinking it to zero in which case it will always succeed?

No. realloc is not guaranteed to suceed even when the new size is zero.

And as I wrote in the other post, I wonder how I can know that.
Can't you just use free()?
Apr 1 '07 #6
ba******@hushma il.com wrote:
On Apr 1, 9:07 pm, "santosh" <santosh....@gm ail.comwrote:
banan...@hushma il.com wrote:
Hi,
I just want to get this right.
A call to realloc() will return NULL on error
The word failure might be more appropriate in this context than the
word error.
and the original memory is left untouched,
both when requesting a larger or a smaller size that the original, right?
Yes.
But a call to realloc() with size set to zero is equivalent to free(),
Semantically yes.
with returns void.
No, it returns an indeterminate value.

What? My manpage says "void free(void *ptr);",
doesn't free return void? By that I meant, free doesn't return
anything.
You're correct. But realloc with a new size of zero either returns a
null pointer, upon failure, or returns an indeterminate value.
Does that mean that a call to realloc() can fail when shinking memory except when
shrinking it to zero in which case it will always succeed?
No. realloc is not guaranteed to suceed even when the new size is zero.

And as I wrote in the other post, I wonder how I can know that.
If the reallocation to zero size suceeds, an indeterminate pointer
value is returned, if it fails, a null pointer value is returned and
the original block is unchanged.

Apr 1 '07 #7
ba******@hushma il.com writes:
But I wonder, if realloc(...,0) fails in C99, how can I know that?
I asked on comp.std.c recently. In C99, realloc returns NULL if and
only if it fails. You'll see it if you read the C99 text without
expecting compatibliity with the C89 realloc(,0) behavior.
How can I know if a returned NULL means that realloc() failed,
or if it is the pointer to the new memory, empty, which is NULL.
Just avoid realloc(, 0).

In theory, you can check '#if __STDC_VERSION_ _ >= 199901L' to see if you
have a C99 compiler. In practice, that doesn't tell you if you have a
C99 _library_: You might have a compiler like gcc which has both C99 and
C89 mode, but uses the system C library in either case.

--
Regards,
Hallvard
Apr 1 '07 #8
On Apr 1, 10:17 pm, "santosh" <santosh....@gm ail.comwrote:
banan...@hushma il.com wrote:
On Apr 1, 9:07 pm, "santosh" <santosh....@gm ail.comwrote:
banan...@hushma il.com wrote:
Hi,
I just want to get this right.
A call to realloc() will return NULL on error
The word failure might be more appropriate in this context than the
word error.
and the original memory is left untouched,
both when requesting a larger or a smaller size that the original, right?
Yes.
But a call to realloc() with size set to zero is equivalent to free(),
Semantically yes.
with returns void.
No, it returns an indeterminate value.
What? My manpage says "void free(void *ptr);",
doesn't free return void? By that I meant, free doesn't return
anything.

You're correct. But realloc with a new size of zero either returns a
null pointer, upon failure, or returns an indeterminate value.
Does that mean that a call to realloc() can fail when shinking memory except when
shrinking it to zero in which case it will always succeed?
No. realloc is not guaranteed to suceed even when the new size is zero.
And as I wrote in the other post, I wonder how I can know that.

If the reallocation to zero size suceeds, an indeterminate pointer
value is returned, if it fails, a null pointer value is returned and
the original block is unchanged.
In that case there is something strange here, if I compile this in gcc

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *ptr = malloc(42);
if (realloc(ptr, 0) == NULL)
printf("realloc failed\n");
return 0;
}

I always get "realloc failed" printed, in both c89 and c99 mode.
I wouldn't expect it everytime as it does here.
Anyway, I will use free() instead, to avoid confusion.
Thanks for the help!
Apr 1 '07 #9
On 1 Apr 2007 11:50:05 -0700, ba******@hushma il.com wrote:
>Hi,
I just want to get this right.
A call to realloc() will return NULL on error and the original memory
is left untouched,
both when requesting a larger or a smaller size that the original,
right?
It returns NULL if the space cannot be allocated. Whether it is due
to an error or not is something else.
>But a call to realloc() with size set to zero is equivalent to free(),
with returns void.
No it is not. Read the last sentence of 7.20.3 again.
>Does that mean that a call to realloc() can fail when shinking memory
except when
shrinking it to zero in which case it will always succeed?
No. Success is not guaranteed.
>
Thanks!

Remove del for email
Apr 1 '07 #10

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

Similar topics

11
3552
by: Eitan Michaelson | last post by:
Hi, Can any one tell me what's wrong with this code? It leaks (acceding to bound checker), when it attempts to reallocate memory. The code is not pure C, but with minor adjustments any C compiler would compile this.
9
2349
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my incHeapSize function, which is supposed to increase the malloced space for an integer array by 1: Heap incHeapSize(Heap aHeap) { int* tempHeap; aHeap->maxSize++; tempHeap = realloc(aHeap->heapArray, aHeap->maxSize);
2
5339
by: collinm | last post by:
hi i expect to find 7 file on a folder... maybe less, maybe more... i can surely put this number to 1... but i think doing some realloc will be expensive... after 7 files, i need to use realoc... somebody could help me to use realloc?
86
4117
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory that was freed by realloc(), but the only way explicitly mentioned in the C89 standard to free memory via realloc() is to realloc() it down to 0 bytes. I had always assumed it would automatically free the previous memory, but is the behaviour...
12
2090
by: subramanian | last post by:
I have taken the following prototype from K & R. void *realloc(void *p, size_t size); Suppose p was earlier allocated by malloc. Suppose I am calling realloc with larger size value. If realloc is successful, will the return pointer be the same as p or will it be different. K & R 2nd edition says "realloc returns a pointer to the new space".
29
7872
by: marvinla | last post by:
Hello! I'm a beginner in C, and I'm having trouble with a pointer-to-pointer reallocation. This piece of code works well, but Valkyrie warns some parts (pointed below), and is breaking my real code. #include <stdio.h> #include <stdlib.h>
9
3796
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
6
2316
by: lithiumcat | last post by:
Hi, maybe you remember me, some time ago I asked about how to store an integer value into a void*, and I learned that doing pointer arithmetic yeilding a pointer outside of an object (except the one- after-last thingy) is undefined behaviour. Actually I was trying to associate a function pointer with a key, through an AVL tree that managed void* data. Function pointers can't be stored in void* (that is, the standard does not garantee...
35
5654
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure aren't in my C books. Interesting functions by they're OT here but this raises to me a question. If a function returns a pointer to a void and and as it's first parameter a pointer to a void. Then should that function's first parameter accept a string...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.