Path: chuka.playstation.co.uk!news From: "John Blackburne" Newsgroups: scee.yaroze.programming.codewarrior Subject: Re: align Date: Sat, 19 Jun 1999 23:16:59 +0000 Organization: PlayStation Net Yaroze (SCEE) Lines: 56 Message-ID: <7kh4ri$33u1@chuka.playstation.co.uk> References: <7k2fvl$hn711@chuka.playstation.co.uk> NNTP-Posting-Host: p-125-virgin12.tch.virgin.net Mime-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Newsreader: Microsoft Outlook Express Macintosh Edition - 4.5 (0410) ---------- In article <7k2fvl$hn711@chuka.playstation.co.uk>, "Jon Prestidge (alias Moose)" wrote: > Can you align items within a structure to a long word boundry somehow? > > I've seen 'align' in the Codewarrior manual but it says it's only for Mac OS > programming. There's also the 'pack' pragma, though I've not used it myself so don't know too much about it's operation.but from the docs this: #pragma pack (4) SVECTOR myVector; #pragma pack(4) should do it. Alternately a method that will work with any compiler is put the SVECTOR in a union. The alignment of a union is the same as that of whichever member has the largest alignment, so just include a 4-byte aligned type, e.g. a long, in your union. It makes your code a bit more complex - basically you trade portability and maintainability (by someone not familiar with CW's arcane pragmas) for simplicity. > The reason is that I've found that 'ApplyMatrixSV' crashes with a fetch > error at 8002d550 if the address of the input SVECTOR is not aligned on a > long-word boundry. That does not sound right. There's no programming reason for a SVECTOR to be aligned any way other than it's natural alignment, i.e. 2 bytes. If it were you would expect it to be noted or specified in the headers. I've encountered this in code as when I inadvertently wrote code which assumed 4-byte alinment for SVECTORS my code quickly broke as the compiler happily puts them on boundaries that are 2-byte but not 4-byte. But the matrix does need to be 4-byte - it's an unusual structure, with a 3 x 3 array of shorts followed a 3 element vector of longs. This enforces 4-byte alignment on the whole thing (and so creates an implicit 2-byte pad in the struct), and functions can use this to speed copying and loading the matrix. As many functions only use the 3x3 array of shorts it may be tempting to just declare such an array, but as this may not be aligned as the libs expect it may crash in such routines. It's possible that you're interpreting such mis-alignment as the SVECTOR's mis-alignment if they're adjacent in memory. Try stepping though the code in assembly, see which pointer is causing it problems, and then check what it is a pointer too - if anything, as a null pointer, or pointer to non-existent memory, can cause similar errors. John