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