Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!well!alcmist From: alcmist@well.UUCP (Frederick Wamsley) Newsgroups: comp.sys.mac.programmer Subject: Re: Pointer arithmetic in C w/ or w/o locked handles Message-ID: <5744@well.UUCP> Date: 20 Apr 88 05:22:47 GMT References: <1988Apr15.125337.100@mntgfx.mentor.com> <10897@tut.cis.ohio-state.edu> Reply-To: alcmist@well.UUCP (Frederick Wamsley) Organization: Whole Earth 'Lectronic Link, Sausalito, CA Lines: 35 Keywords: pointer C arithmetic handles In article <10897@tut.cis.ohio-state.edu> paul@morganucodon.cis.ohio-state.edu (Paul Placeway) writes: >In article <1988Apr15.125337.100@mntgfx.mentor.com> tomc@mntgfx.mentor.com (Tom Carstensen) writes: >< ... > >< HUnlock(hdl); >< length = (long) (ptr - *hdl); >< ... >< If I move the length calculation before I unlock the >< handle, then length becomes 8, and I'm ok. > >I would be _really_ supprised if something like this continued to work >correctly (assuming that it does allready; which it seems not to). > >Personally, I wouldn't HUnlock the handle until you are done with it >(ie. _after_ the length calculation). It is possible (although currently >unlikely) that the block that the handle points to could be moved >between the HUnlock and the last derefrence. Memory blocks can only move if you call a trap in the (documented) set which use the Memory Manager directly or indirectly, or if you make an intersegment call (which can potentially call _LoadSeg). The only problem with dereferencing a handle to unlocked memory under safe conditions is a code maintenance problem. If you or someone else accidentally changes the code so that it can cause memory motion, you suddenly have an intermittent bug. Of course keeping everything locked while in use will prevent a nasty kind of bug, but it's unfriendly to keep anything locked longer than necessary, even if you call MoveHHi first to keep it from fragmenting the heap. Wistfully thinking about hardware memory management, I remain -- Fred Wamsley {dual,hplabs}!well!alcmist;well!alcmist@lll-crg.arpa; CIS 72247,3130; GEnie FKWAMSLEY;ATT Mail fwamsley; USPS - why bother? "I was just being polite, sir" - Lt. Worf