Re: Finding Type of a Void Pointer

From: Erwin S. Andreasen (erwin@PIP.DKNET.DK)
Date: 11/29/97


On Sat, 29 Nov 1997, ;P wrote:

> now, i use the causer pointer to refer to objects and characters so that
> i can remove the events associated with them when the char or obj is
> extracted..in the list event command, i'm trying to give the name of the
> character or object that is associated with that event..so that it will
> look like:

That is going to be a major pain when you have say 5000 events queued - to
have to run through all of them whenever an object or character is
extracted.

> but i can't think of how to determine whether its an object or char..uh..
> any ideas?

Magic numbers. See http://www.abandoned.org/drylock/letters/magic.txt

Basically, you insert a byte-field at the beginning of each event-able
structure, and set it to some known value depending on whether it's a
mob/object/room.

This is also useful for other things - like ensuring that you don't pass
an object when you meant to pass a mob to act().

You will then check the magic number by something like:

if (*(unsigned char*)ptr == MAGIC_CHAR)
{
        ...
}

Oh, this is also useful for tracking memory bugs; when you wonder what the
heck really overwrote this piece of memory you have here, and you see the
first byte is 0xEA, the char magic number.. aha!

This method is BTW, used in the Linux kernel. If it's good enough for the
kernel, it's good enough for me :)


 =============================================================================
Erwin Andreasen   Herlev, Denmark <erwin@pip.dknet.dk>  UNIX System Programmer
<URL:http://www.abandoned.org/drylock/>     <*>         (not speaking for) DDE
 =============================================================================


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST