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
> the list event command, i'm trying to give the name of the
> character or object that is associated with that 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

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

Magic numbers. See

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

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 <>  UNIX System Programmer
<URL:>     <*>         (not speaking for) DDE

     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | |

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