I looked at my copy of n1124, and I didn't see anything about this
particular situation...
What happens if you realloc() to a size of zero?
Implementations are allowed to return NULL on malloc(0), and realloc()
says it reutrns NULL on failure. (And, on failure, the old pointer
has not been freed.)
Is it possible for an implementation to return NULL for realloc(ptr,0)
and have the old buffer freed?
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com> 4 3471
Kenneth Brody wrote:
I looked at my copy of n1124, and I didn't see anything about this
particular situation...
What happens if you realloc() to a size of zero?
Implementations are allowed to return NULL on malloc(0), and realloc()
says it reutrns NULL on failure. (And, on failure, the old pointer
has not been freed.)
Is it possible for an implementation to return NULL for realloc(ptr,0)
and have the old buffer freed?
In C99, no. realloc differs from malloc in that is specified as returning
either a pointer to a new object, or a null pointer if the new object could
not be allocated. A null pointer is not a pointer to an object, so can only
indicate allocation failure, and on allocation failure, the old buffer is
not freed. In C90, however, realloc(p, 0) unconditionally frees p,
regardless of whether it returns a null pointer. There was a thread on
comp.std.c about it a while ago: http://groups.google.com/group/comp....5064cb8f54637/
Harald van Dijk <tr*****@gmail.comwrites:
Kenneth Brody wrote:
>I looked at my copy of n1124, and I didn't see anything about this particular situation...
What happens if you realloc() to a size of zero?
Implementations are allowed to return NULL on malloc(0), and realloc() says it reutrns NULL on failure. (And, on failure, the old pointer has not been freed.)
Is it possible for an implementation to return NULL for realloc(ptr,0) and have the old buffer freed?
In C99, no. realloc differs from malloc in that is specified as returning
either a pointer to a new object, or a null pointer if the new object could
not be allocated. A null pointer is not a pointer to an object, so can only
indicate allocation failure, and on allocation failure, the old buffer is
not freed. In C90, however, realloc(p, 0) unconditionally frees p,
regardless of whether it returns a null pointer. There was a thread on
comp.std.c about it a while ago:
http://groups.google.com/group/comp....5064cb8f54637/
I just re-read that thread (and I even participated in it), but I
don't think it actually settled the issue.
I still see a contradiction between C99 7.20.3.1p1, which applies to
malloc, calloc, and realloc:
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.
(which allows realloc(non_null, 0) to return either a null pointer or
a non-null pointer, and doesn't allow you to determine whether it
succeeded or failed if it returns a null pointer), and C99 7.20.3.4p4,
which applies only to realloc:
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.
(which *doesn't* allow realloc(non_null, 0) to return a null pointer
unless the allocation actually fails).
An implementation can obey both by having all three *alloc functions
with a zero size argument return a null pointer *only* on failure.
The question is whether an implementation of realloc that returns a
null pointer for a zero size argument even on success is conforming.
My impression is that the authors of C99 cleaned up the description of
realloc, but neglected to go back and clean up the earlier description
that applies to all three allocation functions.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Keith Thompson wrote:
Harald van Dijk <tr*****@gmail.comwrites:
>Kenneth Brody wrote:
>>I looked at my copy of n1124, and I didn't see anything about this particular situation...
What happens if you realloc() to a size of zero?
Implementations are allowed to return NULL on malloc(0), and realloc() says it reutrns NULL on failure. (And, on failure, the old pointer has not been freed.)
Is it possible for an implementation to return NULL for realloc(ptr,0) and have the old buffer freed?
In C99, no. realloc differs from malloc in that is specified as returning either a pointer to a new object, or a null pointer if the new object could not be allocated. A null pointer is not a pointer to an object, so can only indicate allocation failure, and on allocation failure, the old buffer is not freed. In C90, however, realloc(p, 0) unconditionally frees p, regardless of whether it returns a null pointer. There was a thread on comp.std.c about it a while ago:
http://groups.google.com/group/comp....5064cb8f54637/
>
I just re-read that thread (and I even participated in it), but I
don't think it actually settled the issue.
FWIW, the original question was settled well enough for Mr. Furuseth, and
also for myself. Doug Gwyn's arguments were pretty convincing.
I still see a contradiction between C99 7.20.3.1p1, which applies to
malloc, calloc, and realloc:
That's 7.20.3p1, actually. :)
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.
(which allows realloc(non_null, 0) to return either a null pointer or
a non-null pointer, and doesn't allow you to determine whether it
succeeded or failed if it returns a null pointer), and C99 7.20.3.4p4,
which applies only to realloc:
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.
(which *doesn't* allow realloc(non_null, 0) to return a null pointer
unless the allocation actually fails).
An implementation can obey both by having all three *alloc functions
with a zero size argument return a null pointer *only* on failure.
And as both paragraphs are normative, an implementation must obey both.
The question is whether an implementation of realloc that returns a
null pointer for a zero size argument even on success is conforming.
If realloc(p, 0) frees p and returns a null pointer, the implementation
violates 7.20.3.4p3-4, so it cannot claim conformance to C99.
7.20.3p1 allows malloc(0) and realloc(p, 0) to return a null pointer, but
does not specify whether this indicates success or failure. In the case of
malloc, it makes no difference. In the case of realloc, 7.20.3.4p4
specifies that it signals a failure, and 7.20.3.4p3 specifies that because
of it, the old object is not freed.
My impression is that the authors of C99 cleaned up the description of
realloc, but neglected to go back and clean up the earlier description
that applies to all three allocation functions.
That's very well possible; the current wording is certainly confusing at the
least.
Groovy hepcat Kenneth Brody was jivin' in comp.lang.c on Tue, 21 Aug
2007 3:43 am. It's a cool scene! Dig it.
I looked at my copy of n1124, and I didn't see anything about this
particular situation...
What happens if you realloc() to a size of zero?
Implementations are allowed to return NULL on malloc(0), and realloc()
says it reutrns NULL on failure. (And, on failure, the old pointer
has not been freed.)
Is it possible for an implementation to return NULL for realloc(ptr,0)
and have the old buffer freed?
It's the same for calloc(), malloc() and realloc(). See the exerpt
below.
-----------------------------------------------------------------------
7.20.3 Memory management functions
1 ... 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.
-----------------------------------------------------------------------
--
Dig the sig!
----------- Peter 'Shaggy' Haywood ------------
Ain't I'm a dawg!! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jonas |
last post by:
Hi,
I'm 99 % sure that Standard C guarantees to do a memory move inside
realloc() in case the new, returned memory block (address) is different than
the original one. Can any C expert confirm...
|
by: dagger |
last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a
bit of trouble using the calloc and realloc calls.
As an example the code snippet:
#include <stdio.h>
int main() {...
|
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...
|
by: James S. Singleton |
last post by:
Thanks everybody for your replies. I gather that:
a) How to obtain the size of the memory region pointed to by ptr in
realloc(ptr, size) is implementation-dependent.
b) Implementing...
|
by: James Brown |
last post by:
Hi all,
I just wanted to make sure I understand realloc
void *realloc( void *memblock, size_t size );
1. when memblock is zero, realloc behaves like malloc
2. when memblock is non-zero, and...
|
by: Deephay |
last post by:
Greetings all,
I have a program that used the realloc() function to change the
allocated size of a buffer, the program works with some arguments, but
with some other arguments, it will show me...
|
by: Robert Seacord |
last post by:
The C standard doesn't say anything about what happens when you call
realloc with a size argument of 0. Both glibc and openbsd appear to
return a valid pointer to a zero-sized object.. e.g. the...
|
by: banansol |
last post by:
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,...
|
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>
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |