realloc(NULL,0) 
Author Message
 realloc(NULL,0)

I am aware that ,if p is a pointer to memory
allocated by malloc(),calloc() or realloc(),
realloc(p,0);  is the same as free(p);
However,
Is realloc(NULL,0) defined?
And would it do the same as  free(NULL)?  ie. do nothing.

Al Bowers                                
Tampa, FL

http://www.*-*-*.com/ ~abowers/index.html



Sat, 08 Jan 2000 03:00:00 GMT  
 realloc(NULL,0)

|> I am aware that ,if p is a pointer to memory
|> allocated by malloc(),calloc() or realloc(),
|> realloc(p,0);  is the same as free(p);

This is correct.  There's more controversy over what "realloc(p, 0)"
returns to the caller.

|> However,
|> Is realloc(NULL,0) defined?
|> And would it do the same as  free(NULL)?  ie. do nothing.

7.10.3.4 The realloc function

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

    The realloc function changes the size of the object pointed to
    by ptr to the size specified by size.  The contents of the
    object shall be unchanged up to the lesser of the new and old
    sizes.  If ptr is a null pointer, realloc *behaves like the malloc
    function* for the specified size.  Otherwise, if ptr does not
    match a pointer earlier returned by the calloc, malloc, or
    realloc function, the behavior is undefined.  If the space cannot
    be allocated, the object pointed to by ptr is unchanged.  If
    size is zero and ptr is not a null pointer, the object it points
    to is freed. [ emphasis mine ]

Now, let's look at your particular scenario.  The above description
makes it clear that such a call would be equivalent to

    malloc(0);

so the behavior depends on how the implementation handles a request
to allocate zero bytes.

Now the Standard says with regards to the allocation functions ".. if
the size of the space requested is zero, the behavior is implementation-
defined; the value returned shall either be a null pointer or a
unique pointer."

So the short answer is yes, it is defined, albeit by the implementation
and not by the language.  What is open for more debate is how useful
the construct actually is. :-)

Regards,

--
Chris Engebretson --- Hughes STX Corporation | Ph#: (605)594-6829
USGS EROS Data Center, Sioux Falls, SD 57198 | Fax: (605)594-6490
Landsat 7 IAS Engineering Team -- http://ltpwww.gsfc.nasa.gov/IAS

Opinions here are not those of Hughes Aircraft, STX, or the USGS.



Sat, 08 Jan 2000 03:00:00 GMT  
 realloc(NULL,0)


Quote:

>I am aware that ,if p is a pointer to memory
>allocated by malloc(),calloc() or realloc(),
>realloc(p,0);  is the same as free(p);
>However,
>Is realloc(NULL,0) defined?
>And would it do the same as  free(NULL)?  ie. do nothing.

No.  Since realloc(NULL, <value>) is the same as malloc(<value>),
it is the same as malloc(0).

The standard allows malloc(0) to either return a unique pointer or a null
pointer. The choice is up to the implementation.  But either way you cannot
dereference the pointer, so from the programmer's point of view, the only
difference is whether or not the returned pointer compares favorably to a null
pointer.



Sun, 09 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. realloc(NULL, size) ?

2. realloc(NULL,100)

3. realloc((char *)NULL,size) - how standard ?

4. What happens when realloc() returns NULL?

5. if (NULL==realloc(ptr, size)) *ptr==???????

6. malloc()/realloc() vs realloc()

7. Realloc failure when downsizing (was Re: Probably a simple realloc() question)

8. NULL v null pointer constant

9. Global pointer is null sometimes even when initialized to some non-null value

10. NULL as a null function pointer

11. confused null pointer with null pointer constant

12. memcpy( NULL, NULL 0 ) - safe or not?

 

 
Powered by phpBB® Forum Software