Pointer Question.

John Hascall hascall at atanasoff.cs.iastate.edu
Thu May 11 08:00:44 AEST 1989


In article <14454 at duke.cs.duke.edu> apte at helios.cs.duke.edu (Jitendra Apte) writes:
>I like to test pointers passed to routines at the beginning of the routine.
>I currently use a macro called CK_PTR() as follows....
 
>This works fine while checking for NULL pointers. However, there are times
>when, due to some other bug in the program under develpment, pointers point out
>into space, nowhere in particular : eg. p = 0x2
 
>Is there a better way of checking pointers so as to detect such problems? Seems
>difficult, because ideally the macro would have to know the limits of address
>space which are "legal".
 
   One possible/partial solution is to always reserve a field in every
   structure which contains an indication of the structure type.  If I
   can ``steal'' from VMS here--all (or most all) the control blocks used
   in VMS start out like this (using a CXB [complex buffer] as an example):

		  struct CXB {
		       struct CXB       *cxb$l_fl;
		       struct CXB       *cxb$l_bl;
		       unsigned short    cxb$w_size;
		       unsigned char     cxb$b_type;
		       unsigned char     cxb$b_subtype;
		       ....
                  };

   The type of the particular structure is always at offset 10 (0x0A), in
   this example it would be DYN$C_CXB (0x1b If I recall correctly).

   Not only can this find most (1 chance in 256 of random error) pointers
   which point at some silly location, it can find pointers which point
   to the wrong type of structure.

   Of course this only works for pointers to structures.


   John Hascall
   ISU Comp Center
   (p.s. VMS isn't written in C, just my ``example'' is)



More information about the Comp.lang.c mailing list