Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Can realloc(p,0) return NULL when p is non-NULL and memory aplenty?

Reply
Thread Tools

Can realloc(p,0) return NULL when p is non-NULL and memory aplenty?

 
 
Harald van Dijk
Guest
Posts: n/a
 
      03-06-2008
On Thu, 06 Mar 2008 19:31:30 +0100, Joachim Schmitz wrote:
> Harald van D?k wrote:
>> On Thu, 06 Mar 2008 18:03:02 +0000, Walter Roberson wrote:
>>> In article
>>> <cb101878-66a6-4c38-8bc4->,
>>> Francois Grieu <> wrote:
>>>> When running the following code under MinGW, I get realloc(p,0)
>>>> returned NULL
>>>> Is that a non-conformance?
>>>
>>> No, it is conformance, and returning non-NULL would be
>>> non-conformance.
>>>
>>> [C89 citation snipped]

>>
>> While you're not at all wrong, please keep in mind that this is one of
>> the areas in which C99 differs from the previous standard. In C99, it's
>> unspecified whether realloc(p, 0) returns a null pointer, but if it
>> returns a null pointer, then p is *not* freed.

> I don't read it mlike this
>
> The realloc function returns a pointer to the new object (which may have
> the same
> value as a pointer to the old object), or a null pointer if the new
> object could not be
> allocated.


7.20.3p1 applies to all allocation functions.
"If the size of the space requested is zero, the behavior is
implementation-defined: either a null pointer is returned, or the
behavior is as if the size were some nonzero value, except that the
returned pointer shall not be used to access an object."

This allows realloc(p, 0) to unconditionally fail (since the text you
quoted specifies that a null pointer return value signifies failure for
realloc).

> allocating 0 bytes can't be too difficult


<nit>realloc(p, 0), if it succeeded, hasn't allocated 0 bytes. It has
allocated one or more bytes, plus whatever information free needs to give
it back later.</nit>

Anyway, even if it weren't specifically allowed to always fail, it could
still for example fail on implementations where if the size is small,
*alloc returns pointers to pre-allocated buckets depending on the
requested size.
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      03-06-2008
Walter Roberson wrote:
> Francois Grieu <> wrote:
>
>> When running the following code under MinGW, I get
>> realloc(p,0) returned NULL
>> Is that a non-conformance?

>
> No, it is conformance, and returning non-NULL would be non-conformance.
>

.... snip C89 quote ...

C99 quote:

7.20.3 Memory management functions

[#1] The order and contiguity of storage allocated by
successive calls to the calloc, malloc, and realloc
functions is unspecified. The pointer returned if the
allocation succeeds is suitably aligned so that it may be
assigned to a pointer to any type of object and then used to
access such an object or an array of such objects in the
space allocated (until the space is explicitly freed or
reallocated). Each such allocation shall yield a pointer to
an object disjoint from any other object. The pointer
returned points to the start (lowest byte address) of the
allocated space. If the space cannot be allocated, a null
pointer is returned. If the size of the space requested is <
zero, the behavior is implementation-defined: either a null <
pointer is returned, or the behavior is as if the size were <
some nonzero value, except that the returned pointer shall <
not be used to access an object. The value of a pointer <
that refers to freed space is indeterminate. <

Note that either a NULL or an individual pointer can be returned.
The pointer to a zero size space can't be dereferenced. You also
can't positively tell a malloc system error from success. So I
advise always allocating (or reallocing) 1 or more bytes.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Falcon Kirtaran
Guest
Posts: n/a
 
      03-07-2008
Francois Grieu wrote:
> 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>
>
> int main(void)
> {
> void *p;
> p = malloc(0);
> if (p==NULL)
> puts("malloc(0) returned NULL");
> else
> {
> p = realloc(p,0);
> if (p==NULL)
> puts("realloc(p,0) returned NULL");
> else
> puts("realloc(p,0) did not return NULL");
> }
> return 0;
> }


In the UNIX manpage for the function, you will find the following text:

realloc() returns a pointer to the newly allocated memory, which is
suitably aligned for any kind of variable and may be different from
ptr, or NULL if the request fails. If size was equal to 0, either
NULL or a pointer suitable to be passed to free() is returned. If
realloc() fails the original block is left untouched; it is not freed or
moved.

Now, it really does beg the question why you are trying to realloc() the
void * to be 0 bytes long. The function you should use for this is
free() (and you will find in the same manpage that realloc(x, 0) and
free(x) are equivalent.

Nevertheless, that behaviour is defined and correct.

--
--Falcon Kirtaran
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-07-2008
CBFalconer wrote:
<snip>
> You also can't positively tell a malloc system error from success.

Probably not with standrd C, but AFAIK POSIX reqires malloc to set errno on
failure.
So if malloc() return NULL, lookup errno, if that is 0, no failure occured.

Bye, Jojo


 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      03-07-2008
CBFalconer <> writes:

> Note that either a NULL or an individual pointer can be returned.
> The pointer to a zero size space can't be dereferenced. You also
> can't positively tell a malloc system error from success. So I
> advise always allocating (or reallocing) 1 or more bytes.


While that's true, I'm not sure I see what practical use it would make
to be able to determine the difference between NULL returned because
of a system error, and NULL returned because you asked for either that
or an unusable pointer.

That being said, I agree about reallocing 1 or more bytes, since doing
0 without knowing that your pointer will be freed is pretty useless.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why use "return (null);" instead of "return null;" ? Carl Java 21 08-24-2006 04:33 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
Can't return null byte with Response.BinaryWrite Vitaly Sedov ASP .Net 0 02-14-2006 10:08 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
How I free memory and return value from that memory area? sam C++ 2 06-27-2003 01:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57