Pointer Question.

Jitendra Apte apte at helios.cs.duke.edu
Thu May 11 01:28:08 AEST 1989


I like to test pointers passed to routines at the beginning of the routine.
I currently use a macro called CK_PTR() as follows.

--------------------------------------------------------------------------
/* Use only if pointer is not expected to be NULL */
#define CK_PTR(_p_) {\
	if (!(_p_)) {\
		fprintf(stderr, "NULL pointer\n");\
		exit(1);\
	}\
}\

void
routine(p)
	struct xyz_s *		p;
{
	CK_PTR(p);
	...
	...
}
--------------------------------------------------------------------------

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".

Fangs.
Jitendra.
(Internet) apte at cs.duke.edu; (UUCP) {decvax|ihnp4|allegra}!duke!apte
"Let us save the kid from abortion, so that he can grow up to steal bread,
 rob banks and murder people. Then we can kill him in the electric chair".



More information about the Comp.lang.c mailing list