Jos Timanta Tarigan wrote:
> Hi,
>
> i notice this line:
>
> #pragma pack(push,1)
> struct TGAHeader {
>
> bla bla bla...
>
> };
> #pragma pack(pop)
>
> what is the definition of pragma exatly? i use this struct as a header that
> will be written in a file. when i delete this line, the file can not be
> opened by the application (corrupted). can any1 help me?
> *googled but not understand :(
>
> thanks in advance
pack() is a VC++ specific #pragma directive. It declares the padding of
the structure's variables. Assuming a 32-bit target, VC++ will pad each
variable to a 32-bit boundary by default regardless of variable size.
The #pragma pack() directive alters this behavior. The example above is
fairly common and forces padding to occur to the nearest byte.
Do:
printf("%u", sizeof(TGAHeader));
Then run the program with and without the #pragma's to see the
difference in structure size. Smaller structures do not mean faster,
but if you are attempting to read a whole structure in from a file in a
single read, packed structures can be the way to go. IMO, you should
always filter data for validity before loading a structure - too many
software exploits have happened because some programmer took the easy
route and simply loaded a structure and then immediately used it without
checking it for validity.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/