pointer -> long conversion

Morris Keesan keesan at bbncca.ARPA
Tue Jul 10 05:37:13 AEST 1984


----------------------

    On machines where pointers are shorter than longs, should the pointer
sign-extend when being converted to long (assume all pointers the same size
for simplicity -- on machines with more than one size of pointer, the question
applies to those types of pointers which are shorter than longs)?  This   
question arises because of some code we came across which is converting
pointers to long in various ways (don't ask why -- it's a long story, but the
reasons turn out to be mostly valid), and the compiled code is generating
sign-extension.  At first, this appears to be wrong, but on second thought I'm
not sure.  K&R says (section 7.14, p.192) "The compilers currently allow a
pointer to be assigned to an integer, an integer to a pointer, and a pointer to
a pointer of another type.  The assignment is a pure copy operation, with no
conversion."  But what does "pure copy" mean, when the objects are of different
sizes?  Is there any reason to prefer non-sign-extension over sign-extension,
or vice-versa?  Is there any reason why a C programmer should legitimately care
whether sign-extension occurs in these cases?  Please no flames about whether
converting pointers to longs is a reasonable operation.  I'm just trying to
figure out what the compiler should do, given that the operation is allowed.
-- 
					Morris M. Keesan
					{decvax,linus,wjh12,ima}!bbncca!keesan
					keesan @ BBN-UNIX.ARPA



More information about the Comp.lang.c mailing list