Question: Question:
At some point
p = malloc(n*sizeof(Type));
…
p = realloc(p, ++n*sizeof(Type));
…
free(p);
I saw a code like (not the actual code),
The problem (I want to talk about now) is that n starts from 0.
I thought malloc(0)
?, so I looked it up a bit.
According to the draft of C99,
7.20.3 Memory management functions 1
… If the size of the space requested is zero, the behavior is
implementationdefined: 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.
In my understanding (interpretation with poor English ability, please point out if you make a mistake, * the same applies below)
"If 0 is requested for size, the operation is implementation-defined,
A null pointer or a non-zero pointer that is inaccessible as an object memory block is returned. "
Also, the operation of free is
The free function causes the space pointed to by ptr to be
deallocated, that is, made available for further allocation. If ptr is
otherwise, if the argument does not
match a pointer earlier returned by the calloc, malloc, or realloc
function, or if the space has been deallocated by a call to free or
realloc, the behavior is undefined.
"Free frees the area pointed to by the pointer. If the pointer is FULL, do nothing, if the pointer is not a pointer returned by malloc, calloc, realloc, or if the area has already been freed by free, realloc. Is undefined "
And the behavior of realloc
The realloc function deallocates the old object pointed to by ptr and
returns a pointer to a new object that has the size specified by size.
The contents of the new object shall be the same as that of the old
object prior to deallocation, up to the lesser of the new and old
sizes. Any bytes in the new object beyond the size of the old object
have indeterminate values.
If ptr is a null pointer, the realloc
function 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, or if the space has been
deallocated by a call to the free or realloc function, the behavior is
undefined. If memory for the new object cannot be allocated, the old
object is not deallocated and its value is unchanged.
"The realloc function frees the old area and returns a pointer to a new area of the specified size …. If the pointer is NULL, it behaves like malloc. If the pointer is returned with malloc, calloc, realloc. The behavior is undefined if it is not a pointer or if the area has already been freed by free, realloc …. "
So the introduction has become longer, but
In case of malloc(0)
, NULL or non-zero pointer that cannot be used is returned (which is returned is implementation definition).
So,
If malloc(0)
returns NULL
, there should be no problem.
What I was curious about was the case of "returning an unusable nonzero pointer".
It's okay that free releases a pointer to the value returned by malloc,
I think realloc doesn't default to "if an unusable nonzero pointer is returned".
Personally, I think it should behave in the same way as when NULL
, but it is not included.
(The operation of being freed when a new area is secured?)
It can be said that there is an implicit claim to accept the return value of the malloc system because it is undefined except for the return value of the malloc system.
I mean,
"Null or unusable non-zero pointer is returned (which is implementation-defined)"
In the first place, "the operation when size 0 is specified is implementation definition", so isn't it written?
Please tell me what to think with detailed human rationale (or different interpretation, such a description elsewhere …).
Answer: Answer:
I tried to translate it a little more accurately.
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.It behaves as if a non-zero size was requested, except that it returns a null pointer or the returned pointer cannot be used to access the object.
It is said that it is the same as the return value when a size other than 0 is passed to the argument of malloc
except that it cannot be accessed, so there is no problem in passing it to the argument of realloc
.