Pointer Comparison and Portability

tps at sdchem.UUCP tps at sdchem.UUCP
Thu Feb 19 17:56:34 AEST 1987


In article <541 at viper.UUCP> john at viper.UUCP (John Stanley) writes:
>In article <636 at sdchema.sdchem.UUCP> tps at sdchemf.UUCP (Tom Stockfisch) writes:
> >
> >That is, even on a segmented architecture, when pointers get passed to a
> >subroutine, they MUST have distinct addresses -- how else can the subroutine
> >know what area of memory to access?
> >
>
>  Tom, on segmented arcitecture machines you -could- have two distinctly
>different pointer values which point to the same memory space...
>...
>  In the example he gave:
>	  char p[16], q[16];
>...
>  This means the address of &p[0] could be 0001:0000 (segment:offset) and the
>address of &q[0] could be 0002:0000.  Now, given that a compiler -could-
>allocate and address the two arrays in this manner, the address for the
>memory location &p[17] (incorrect, but technicaly a legal memory reference)
>would be 0001:0010.  The byte of memory addressed by q[0] and p[17] would
>be the exact same byte, but the two pointers 0002:0000 and 0001:0010 would be
>different.

&p[17] is illegal (or at least undefined) in C,
given the above definition.
If I compared this to any
other pointer value (even inside p[]) I
would not expect to get anything
well defined.

>  Getting back to the original starting question asked by Neil Webber:
>>>Consider the following C function:
>>>        same_char (p, q)
>>>        char *p;
>>>        char *q;
>>>        {
>>>                return (p == q);
>>>        }
>>>Does this function only return a non-zro value when p and q point
>>>to the same physical character?
>  The answer to the exact wording of the question is YES...  However....
>saying that the function will ONLY return a non-zero value when the pointers
>match is not the same as "Does the function ALWAYS return a non-zero value
>when p and q point to the same physical character?"  The answer to the latter
>question is NO.

Judging from your above
comments, I assume that what
you mean by this is that if
you pass &p[16] and &q[0] to
same_char(), they might refer
to the same physical memory
but have a different value.
Since there is no guarantee in
C that adjacent definitions
wind up adjacent in memory, I
would say the result of
same_char( &p[16], &q[0] )
would be undefined (by C).  It
would make no more sense to
argue about what same_char()
should return in this case
than if you had called
	
	char	*r =	p;
	same_char( r++, r++ )

So I would say that if you
pass *defined* pointer values
to same_char() it will return
1 if and only if they refer to
the same memory location, and
will return 0 otherwise.

|| Tom Stockfisch, UCSD Chemistry	tps%chem at sdcsvax.UCSD



More information about the Comp.lang.c mailing list