>--- Forwarded mail from [EMAIL PROTECTED]

>[EMAIL PROTECTED] on 05/03/2000 12:44:09 AM
>>>I was looking at that, too.  I tend to feel that 0-byte allocations should be
>>>allowed (returning NULL) and, if systems' frees don't handle NULL properly,
>>>there should be an xfree() that'll do so.
>>
>>I can see the occasional need to allocate a unique pointer that points to
>>nowhere but is distinguishable from any other pointer, even NULL.  Having a
>>malloc-like function that does this (allocate a block of memory at least 0
>>bytes long and return a pointer to it) is useful, and if it's implemented as
>>a wrapper that increments a 0 size then so be it.
>>
>>This practice is typically used when there's a distinction to be made
>>between an "unset" value and a "set but unspecified" value.  An example
>>of such data in CVS are commit comments:  Comments can be prompted for if
>>the string is unset.  Or, an empty comment can be stored (without a prompt)
>>if the string is set but unspecified.
>>
>>This is a somewhat contrived example because for strings this distinction
>>is made by using a NULL pointer for an unset value, and a pointer to an
>>empty string for a set but unspecified value.  But for structures the
>>distinction is a little harder to implement, and special pointers are
>>one economical way to do it.  Timestamps are another type of data where
>>this distinction might be important, in many applications.

>I don't understand why anyone would ever request a zero-byte allocation for a
>struct nor how they'd be able to tell it apart from a "good" allocation.  IMHO,
>the way to perform the above would be to have an "isValid" field within the
>struct.

An "isValid" field isn't valid if the structure isn't initialized properly.
When this is a concern, the value of the pointer itself becomes significant
(like it is with the NULL pointer, but when its value must also be
distinguishable from NULL).  In such cases, a pointer to block of zero bytes
makes sense to some people.  (In some such cases, it's just as valid to assign
to the pointer the address of a statically allocated structure.  But sometimes
it's not known in advance how many such pointers are needed, so the structures
must be dynamically allocated.)

As for how to tell it apart from a "good" allocation, the pointer is usually
stored in some well-known place at the time the allocation is done, usually
when a program or structure is being initialized.  An arbitrary pointer's
value is compared with that stored in the well-known place when the special
value is of interest.

Anyway, this is getting into generic programming practice and is out of the
scope of info-cvs.  So I'll shut up now....

>--- End of forwarded message from [EMAIL PROTECTED]

Reply via email to