[comp.lang.c] Different pointer representations on the same machine

evil@arcturus.UUCP (Wade Guthrie) (02/14/89)

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.


Wade Guthrie
evil@arcturus.UUCP
Rockwell International
Anaheim, CA

(Rockwell doesn't necessarily believe / stand by what I'm saying; how could
they when *I* don't even know what I'm talking about???)

swh@hpsmtc1.HP.COM (Steve Harrold) (02/15/89)

Re: Different pointer sizes

In MSDOS, try compiling a program in the so-called medium model or in the 
compact model.  The former has function pointers of 32 bits and data pointers
of 16 bits; the latter has 16 bit function pointers and 32 bit data pointers.

henry@utzoo.uucp (Henry Spencer) (02/16/89)

In article <3675@arcturus> evil@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...

The usual reason for this is that the machine was designed as a word-
addressed box and does not easily support pointer-to-character.  This
means that pointer to character needs enough extra bits to address a
character within a word.

On machines with wide words, the extra bits can often be found within a 
pointer, as "unused" bits; then one just needs special code for handling
pointer-to-character that (a) picks those bits out, (b) uses the rest of
the pointer to access a word, and (c) uses the picked bits to pull a
character out of that word.  The PDP-10 and the Cray-1 are examples of
such machines (the 10 is an unusually favorable case because it has some
instructions that do roughly what's wanted).

On machines with narrow words (e.g. 16-bit word-addressed machines), the
situation is much stickier, because there usually aren't any spare bits
in a pointer.  Then one may have to add a whole extra word to the
representation of pointer-to-character to contain the extra bit(s), with
appropriate changes in code generation anywhere such pointers are used
or moved around.  Some of the smaller Data General machines are like this,
as are other minicomputers of similar vintage.

Pointer-to-short can also need such arrangements on word-addressed machines
with fairly wide words.
-- 
The Earth is our mother;       |     Henry Spencer at U of Toronto Zoology
our nine months are up.        | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

daveb@geaclib.UUCP (David Collier-Brown) (02/16/89)

From article <3675@arcturus>, by evil@arcturus.UUCP (Wade Guthrie):
> 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.

  Sure.
  Honeywell DPS-6: word-addressed machine with a facility to use
	an offset (in bytes) into a location specified in words.
	int	*ip ::= a register containing a word address
	char	*cp ::= one register containing a word address
		        another containing an offset
  In larger models the word address could be a register-pair, giving
32 bits for an int *, 48 for a char *.


--dave (the above has changed over time) c-b
-- 
 David Collier-Brown.  | yunexus!lethe!dave
 Interleaf Canada Inc. |
 1550 Enterprise Rd.   | He's so smart he's dumb.
 Mississauga, Ontario  |       --Joyce C-B

swilson@thetone.Sun.COM (Scott Wilson) (02/17/89)

In article <3675@arcturus> evil@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@sun.com
Sun Microsystems	uucp: ...!sun!swilson
Mt. View, CA

gregg@ihlpb.ATT.COM (Wonderly) (02/17/89)

In article <3675@arcturus> evil@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...

The middle model of the typical 80x86 C compiler is a great example.
Typically, pointers to text are FAR (selector/segment plus offset) while
pointers to data are NEAR (just offset).  While there is not a model
(that I have seen) that is the other way, it is a conceivable alternative.

-- 
Gregg Wonderly                             DOMAIN: gregg@ihlpb.att.com
AT&T Bell Laboratories                     UUCP:   att!ihlpb!gregg

bobmon@iuvax.cs.indiana.edu (RAMontante) (02/17/89)

(Apologies to flat-address_space fans and C purists...)

[ Wade Guthrie asks for examples of computers using different
representations for different pointer types.  Then... ]

gregg@ihlpb.ATT.COM (Wonderly) <9626@ihlpb.ATT.COM> :
-The middle model of the typical 80x86 C compiler is a great example.
-Typically, pointers to text are FAR (selector/segment plus offset) while
-pointers to data are NEAR (just offset).  While there is not a model
-(that I have seen) that is the other way, it is a conceivable alternative.

As a matter of fact, the "conceivable alternative" (called Compact in
TC), is the one that makes sense to me.  Most of the interesting
programs I write need less than 64K of text to massage a couple hundred
kilobytes of arrays (all the array I can fit into memory).  I've never
even used the medium model.

The oft-maligned NEAR/FAR keywords in the MSC and TC compilers exist to
facilitate linking modules that are compiled in different models -- this
is a case of having to cope with different representations for the
_same_ types of pointers.  (Although prototyping avoids some goofups, and
the rest usually -- not always -- turn up as linker errors.)
--
"Aren't you sorry you didn't hit 'n'?"

seanf@sco.COM (Sean Fagan) (02/19/89)

In article <1989Feb15.200416.15836@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>In article <3675@arcturus> evil@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...
[Henry talks about word-addressable machines]

As Henry said, machines like, oh, Crays and Cybers, which are
word-addressible, can stuff more than one char per word, and, for maximum
storage efficiency, you use a { word *addr; int offset} combination for char
*'s.
There are also machines (Primes spring to mind) which have pointers which
are the same for all *data*, but in which, say, function pointers will be of
a different size.

Lastly (the reason for my posting), I go back to Cybers.  60-bit words,
60-bit data registers, 18-bits of address.  A <whatever> * on that machine
is 18-bits, while, for convenience sake, all data types are said to be
60-bits.  This breaks code that even most careful programmers write (when
you print out a pointer, for example, do you typecast it do long and use a
%ld format?)...

-- 
Sean Eric Fagan  | "What the caterpillar calls the end of the world,
seanf@sco.UUCP   |  the master calls a butterfly."  -- Richard Bach
(408) 458-1422   | Any opinions expressed are my own, not my employers'.

scs@adam.pika.mit.edu (Steve Summit) (02/22/89)

In article <2310@scolex.sco.COM> seanf@scolex.UUCP (Sean Fagan) writes:
>when you print out a pointer... do you typecast it to long and
>use a %ld format?

No, I just use %p.

                                            Steve Summit
                                            scs@adam.pika.mit.edu

charlie@vicorp.UUCP (Charlie Goldensher) (02/24/89)

In article <9382@bloom-beacon.MIT.EDU>, scs@adam.pika.mit.edu (Steve Summit) writes:
> In article <2310@scolex.sco.COM> seanf@scolex.UUCP (Sean Fagan) writes:
> >when you print out a pointer... do you typecast it to long and
> >use a %ld format?
> 
> No, I just use %p.

That's fine if you don't expect to port the code to another machine.
But be aware that not all versions of printf provide the %p option.
I typecast the pointer to a long and use 0x%lx.  (I find the hex
notation more useful for pointers than the decimal notation.)