Path: utzoo!attcan!uunet!samsung!uakari.primate.wisc.edu!sdd.hp.com!ucsd!helios.ee.lbl.gov!ux1.lbl.gov!beard From: beard@ux1.lbl.gov (Patrick C Beard) Newsgroups: comp.sys.mac.programmer Subject: Re: StripAddress and pointer arithmetic Summary: Only addresses which are master pointers. Message-ID: <5697@helios.ee.lbl.gov> Date: 26 May 90 21:22:21 GMT References: <1990May25.194025.9751@csrd.uiuc.edu> Sender: usenet@helios.ee.lbl.gov Reply-To: beard@ux1.lbl.gov (Patrick C Beard) Organization: Berkeley Systems, Inc. Lines: 40 X-Local-Date: 26 May 90 14:22:21 PDT In article <1990May25.194025.9751@csrd.uiuc.edu> bruner@sp15.csrd.uiuc.edu (John Bruner) writes: #I've just read the technical note on StripAddress (#213, April 1990). #On the second page under the (sub)heading "Ordered Address Comparison" #it says # # If you need to sort by address or do any other kind of # ordered address comparison, you need to call _StripAddress # on each address before doing any ordered comparisons # (>, <, >=, <=). Remember, even though the CPU only uses # the lower 24 bits in 24-bit mode, it still uses all 32 bits # when performing arithmetic operations. # #Taken literally, this means that it is unsafe to write # # struct something *p, things[64]; # # for (p = things; p < things + 64; p++) # ... # #because one can't rely upon the comparison "p < things+64" working. Ok, time for a clarification. Note that your example implies variables that are allocated on the stack. This code will always work. Address arithmetic only has problems when you are dealing with addresses that might be dereferenced handles, or "master pointers". This is because the *current* memory manager keeps state information in the high byte (actually high nibble) of master pointers (i.e. if a handle is locked, purgeable, or a resource). These bits will throw off address comparisons obviously, and so StripAddress is in order. In general, addresses are clean. Examples of where they might not be include: dereferenced handles, code resources dereferenced then called will put garbage in the pc, and so should be stripped before being called. A 32-bit operating system will make all this stuff go away. Let's hope we get it soon. ------------------------------------------------------------------------------- - Patrick Beard, Macintosh Programmer (beard@lbl.gov) - - Berkeley Systems, Inc. "..............Good day!" - Paul Harvey - -------------------------------------------------------------------------------