Regards
Starwynd
Regards
Starwynd
--- Official D Blog | Learning D | The One With D | D Bits
#pragma pack(push, original)
#pragma pack(1)
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.
- Splat
--- Official D Blog | Learning D | The One With D | D Bits
- Splat
Are you sure you didn't have a regular int somewhere where you needed a short int or something?
Mason McCuskey
Spin Studios
www.spin-studios.com
[This message has been edited by mason (edited October 26, 1999).]
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.
-Gilderoot
[This message has been edited by Gilderoot (edited October 26, 1999).]
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.
- Splat
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).]
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.
--- Official D Blog | Learning D | The One With D | D Bits
Thanks.
--- Official D Blog | Learning D | The One With D | D Bits