Different pointer representations on the same machine

Scott Wilson swilson at thetone.Sun.COM
Fri Feb 17 04:44:06 AEST 1989


In article <3675 at arcturus> evil at arcturus.UUCP (Wade Guthrie) writes:
>I have seen references on the net to computers which have different
>representations for different types of pointers (even some computers
>which represent different types of pointers as different sizes).
>
>Could I have some examples of machines along with the pointer types and
>(if possible) sizes and representations?  Thanks.

At my last job I had the pleasure of working in a group creating a
C development environment for a computer known as the TP1.  The TP1 was
the product of Advantest (a Japanese maker of test equipment) and was
reportedly a decendent of some Data General machine.  The TP1 had the
interesting quality that memory references were made using byte addresses
or word addresses.  A byte address referring to the same point in memory
as a word address would have double it's value.  What this meant for the
C programmer was you had to be very careful about casts as casts between
char pointers and non-char pointers would result in a doubling or halving
of the pointers.  A typical mistake would be to pass a structure pointer
to bzero().  Since bzero would be expecting a char pointer, passing it
a struct pointer that had not been cast would result in the wrong area
being cleared.  As another example consider the problem of finding the
byte offset of a structure element:

        struct foo {
                char a;
                int b;
                double c;
        };

        main()
        {
                int offset;

                offset = (int) &((struct foo *) 0)->b;
                printf("offset of b is %d\n", offset);
         }

This would be sufficient for most machines, but for those were casting
an int pointer to a char pointer results in a change in value, it isn't.
The line needs to be changed to:

                offset = (int) (char *) &((struct foo *) 0)->b;

Now whether or not this is portable under ANSI, I don't know.  By the
way, the motivation for this is to recreate a struct pointer given a pointer
to an element known to be within it.  For instance given an int pointer
how do we get a foo pointer?  Like this:

        #define BOFFSET       ((int) (char *) &((struct foo *) 0)->b)
        #define ip_to_fp(ip)  ((struct foo *) ((char *) ip - BOFFSET))

        struct foo *
        which_foo(ip)
                int *ip;
        {
                return(ip_to_fp(ip));
        }

Again, I'm not sure how portable or ANSI compliant this really is.


--
Scott Wilson		arpa: swilson at sun.com
Sun Microsystems	uucp: ...!sun!swilson
Mt. View, CA



More information about the Comp.lang.c mailing list