Pointing pointers at varying types of objects.

David M. LaRocque larocque at jupiter.crd.ge.com
Thu May 3 01:29:29 AEST 1990


I am currently involved in translating a large Lisp 
program into C.  Much of the code has already been 
translated by another person and something they did
has me concerned.  Since in Lisp variables are not
typed, any variable can have any Lisp object as its
value.  To deal with this problem my predecessor
wrote code that would perform operations on "generic"
data.

To illustrate, one might have a structure named "employees"
and another named "books".  One would like to have
a balanced binary tree of "employees", and a balanced
binary tree of "books" since one will be searching
for employees and books frequently and independently.  It
is convenient to write the binary tree routines so that
they will operate on any type of data.  To do this a 
pointer to the structure (to be added, deleted,
or searched for) is passed to the routine along with
a function pointer which is used to perform comparisons
among the elements.  This method has worked quite
effectively for this project since we have so many
different types of structures.

My concern is this: on page 44-45 of _C Wizard's Program-
ming Reference_ by W. David Schwaderer he states: "Con-
trary to popular understanding, pointer sizes on some 
machines vary by the type of object pointed to. Moreover,
even if pointer sizes are the same size for different
objects, their internal structure may be different, again
depending on the particular object pointed to.  Hence, 
it is inadvisable to indiscriminately point pointers at
varying types of objects and expect portable or even
correct results."   This is exactly what we are doing.
He goes on to state that not only will this make lint
complain, the following may result in lethal errors:

1. object assignment and referencing including promotion,
   demotion, conversion and sign extension

2. pointer arithmetic (scaling)

3. structure/union member selection

4. evaluation of an expression such as sizeof(ptrx).

We don't perform any of the above operations and so far
haven't had any problems.  The system is running on a
Sun 3.  My question is if I avoid doing the 4 above
operations will I be safe?  Furthermore are there other
issues that I may have overlooked that may compromise
the portability of my system if I use this method?

Thanks for any help,
Dave

/**************************************************
 * larocque at crd.ge.com        (518) 387-5805
 * ...!crdgw1!cetus.crd.ge.com!larocque
 **************************************************/



More information about the Comp.lang.c mailing list