Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!ginosko!usc!henry.jpl.nasa.gov!elroy.jpl.nasa.gov!cit-vax!tybalt.caltech.edu!palmer From: palmer@tybalt.caltech.edu (David Palmer) Newsgroups: comp.sys.mac.programmer Subject: Re: Integer Size problem Message-ID: <11689@cit-vax.Caltech.Edu> Date: 22 Aug 89 22:03:50 GMT References: <15141@dartvax.Dartmouth.EDU> Sender: news@cit-vax.Caltech.Edu Reply-To: palmer@tybalt.caltech.edu.UUCP (David Palmer) Organization: California Institute of Technology Lines: 92 In article <15141@dartvax.Dartmouth.EDU> xxiaoye@eleazar.dartmouth.edu (Xiaoxia Ye) writes: >Would someone explain the following weirdness? > >the WIND resource' first 8 bytes are its positions on the screen: top, >left, bottom and right. I tried to save the window postions and so to >write to the first 8 bytes of the WIND resource. > >I have my rectange declared as (it's in THINK C): >Rect myRect; >and I get a handle to the WIND resource: >myHandle=GetResource('WIND',MY_WIND_RES_ID); > >after I lock the Handle and do the following to assignment to the >resource: >**myHandle=myRect.top; >**myHandle+=2; Do you mean '*myHandle += 2;'? (I assume you are typing this from memory, otherwise you are doing the equivalent of '**myhandle = myRect.top + 2;') >**myHandle=myRect.left >... blah blah and did this for all four coordinates. >and do a ChangedResource, and UpdateResFile, etc ... DON'T DO THAT!!! You should NEVER change the value of a master pointer (singly indirected handle) The safe way to do this is: char *mypointer; myHandle = getResou... HLock(myHandle); myPointer = *myHandle; *myPointer = myRect.top; myPointer += 2; *myPointer = myRect.left; ...blah blah blah This is 'safe', and equivalent to your code, but it is wrong. > >What happened was: >say that I have the following eight bytes in my WIND resource (in Hex): >0044 0043 0056 0098 >and say that myRect.top = 0x60, myRect.left=0x60, myRect.bottom=0x80 and >myRect.right=0x80. > >and when I examined what was written to the resource, I found out that >first 8 bytes of WIND resource became: >6044 6043 8056 8098 > >in another words, if I just do an integer assignment, I don't get 00 >padding and have the proper alignment. Why?? A handle is just a doubly indirect pointer to a char. '**myHandle = myRect.top' means take the integer myRect.top, and put it in the character pointed to by the master pointer pointed to by myHandle. However, when you put something in a character, you are storing it in a single byte in memory. The second byte is a totally separate character. A correct way to do it would be to make the assignments with: *(int *)myPointer = myRect.top; a better way would be to change that 'int' to a 'short', so this will work on a compiler with 32-bit ints. A better way, clearer and probably faster, however is to just do: **(rect **)myHandle = myRect; You don't have to lock anything down, it is a single easy to understand instruction. This is still not good enough, however. The very best way, however, is to assign the actual eleemnt of the window structure you want, without having to rely on it being the first element of the structure. I don't have Inside Mac here, but the code should go something like: WindowHandle myWHandle; myHandle=(WindowHandle)GetResource('WIND',MY_WIND_RES_ID); /* ^ This cast takes ZERO CPU time to execute */ **myhandle.boundrect = myRect; /* ^ or whatever the element name is */ (followed by changedresource, etc.) David Palmer palmer@tybalt.caltech.edu ...rutgers!cit-vax!tybalt.caltech.edu!palmer "Direct quotes don't have to be exact, or even accurate. Truth is as irrelevant to a newspaper as it is to a court of law" - Judge Alarcon, 9th circuit court of appeals (paraphrased)