Nils Weller <me@privacy.net> wrote:[color=blue]
> On 2005-12-01, S.Tobias <siXtY@FamOuS.BedBuG.pAlS.INVALID> wrote:[color=green]
>> Nils Weller <me@privacy.net> wrote:[color=darkred]
>>> On 2005-11-30, S.Tobias <siXtY@FamOuS.BedBuG.pAlS.INVALID> wrote:[/color]
>>
>> ...[color=darkred]
>>>> Yes, you're right, I was wrong. `realloc(p,0)' is *not* equivalent
>>>> to `free(p)' (for non-null `p'). I knew it once, but forgot.
>>>
>>> Most of us do not use C99. The rules in C89 are different; In C89
>>> realloc(p, 0) is in fact equivalent to free(p);
>>>[/color]
>> Well, I thought they were the same in both versions. Could you
>> expand on the equivalence, please?
>>
>> My understanding has been that `realloc(p,0)' may return non-null
>> pointer. My interpretation of the last sentence in the "Description"
>> (in the C89 draft) is that the object pointed by `p' is always freed,
>> irrespective whether the allocation of zero bytes succeeds or not.[/color]
>
> The realloc function changes the size of the object pointed to by
> ptr to the size specified by size. [...] If size is zero and ptr is
> not a null pointer, the object it points to is freed.
>
> The final sentence overrides the first one: The size of the object is
> not changed, but *instead* the object is freed. Or else which object's
> size should realloc() change, since the only object we had was freed?[/color]
[snip]
Yes, I agree. Thank you.
I didn't find it in the list of changes in the Standard that the semantics
for `realloc' were changed (but then perhaps it wasn't considered to be
a "major" change). Here's what the Rationale says:
# 7.20.3.4 The realloc function
# A null first argument is permissible. If the first argument is not
# null, and the second argument is 0, then the call frees the memory
# pointed to by the first argument, and a null argument may be returned;
^^^^^^
# C99 is consistent with the policy of not allowing zero-sized objects.
#
# A new feature of C99: the realloc function was changed to make it
# clear that the pointed-to object is deallocated, a new object is
# allocated, and the content of the new object is the same as that of
# the old object up to the lesser of the two sizes. C89 attempted to
# specify that the new object was the same object as the old object but
# might have a different address. This conflicts with other parts of
# the Standard that assume that the address of an object is constant
# during its lifetime. Also, implementations that support an actual
# allocation when the size is zero do not necessarily return a null
# pointer for this case. C89 appeared to require a null return value,
^^^^^^^^
# and the Committee felt that this was too restrictive.
It seems that the wording went against the intentions of the Committee,
but it's the words that matter in the end.
--
Stan Tobias
mailx `echo
siXtY@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`