Jump to content
Posted 04 November 1999 - 03:01 AM
Posted 24 October 1999 - 01:11 AM
Posted 24 October 1999 - 04:54 PM
Posted 25 October 1999 - 06:16 PM
#pragma pack(push, original)
right before the structure definition and:
#pragma pack(pop, original)
right after, and that will change the struct alignment to 1-byte, which is exactly what you need.
Posted 26 October 1999 - 06:07 AM
Posted 26 October 1999 - 11:56 AM
Posted 26 October 1999 - 12:15 PM
Are you sure you didn't have a regular int somewhere where you needed a short int or something?
[This message has been edited by mason (edited October 26, 1999).]
Posted 26 October 1999 - 01:14 PM
The default packing size for Win32 (or 95/NT) is at 8 byte boundries.
If you are using Win 3.X the packing size is at 2 byte boundries, which would have kept the size at 18.
[This message has been edited by Gilderoot (edited October 26, 1999).]
Posted 26 October 1999 - 01:44 PM
So, say we have a short (2 byte). Ideally, we want its address to be a multiple to 2 (aligned). Or a long (4 bytes) on a multiple of 4. What 8-byte alignment says is that: "compiler, you can align any data type that is less than or equal to 8 bytes"
So if it gets an 8 byte data member that is off by one byte forward, it will add its maximum of 7 bytes padding to get that type on an 8 byte boundary.
With the TGA header, the first short is 3 bytes in (unaligned). There goes one byte padding. Then, after than padding, the third short is 9 bytes in, so we have another byte of padding. This pushes the key elements of width, height, and bits per pixel exactly one short back, which really messes up your header ;(
However, setting the compiler's packing settings fixes all this. In fact, in the Windows header files they use this setting for several key structures that are commonly read in from files.
Oh yeah, Gilderoot, the packing size of 2 bytes would NOT work because the compiler would still align the shorts (which are 2 bytes)
[This message has been edited by Splat (edited October 26, 1999).]
Posted 26 October 1999 - 03:17 PM
Of course, when I made the call with an 18 instead of sizeof(TGA_HEADER), the results were fine for the first few entries, but got out of whack near the middle. Now I understand why, thanks to the explanation from Splat.