On Tue, Jan 6, 2009 at 1:13 PM, Jos Timanta Tarigan
<[email protected]> wrote:
> hi,
>
> thanks all, but can some1 sum it up in a more 'noobish' way? :D
To pick one implementation:
Every item in the struct takes up 1, 2 or 4 bytes (more if there's an
array in there.)
Things run faster, however, if items start on a memory address of a
multiple of 4.
So if you have a struct with things that take up 1 or 2 bytes, then
the compiler, by default, 'pads' them out with 'invisible' bytes so
that the next item in the struct starts on a multiple of 4 (AKA
'boundary'):
struct x{
char item1; // takes one byte.
char invisible[3]; // the compiler effectively silently inserts
this to put the next item at the start of a boundary
short item 2; // takes two bytes
char invisible[2]; // another silent insert
char item 2; // one byte
char invisible[3]; // another silent insert
.. and so on.
}
What your pragma is doing is telling the compiler NOT to put that
invisible padding in and 'scrunch'/compact everything up as tight as
possible.
[...]
> fwrite(&header, sizeof(TGAHeader), 1, fp);
As I suspected - it's writing the whole struct at once. You'll find an
fread() elsewhere in the program with similar arguments.
> my question is what is this pragma code do? how it can affect my writing file?
If the file is written with a program with the pragma present, then
there will be no 'gaps' between items.
If it's subsequently read with a program compiled with the pragma
absent, then when the 'compacted' struct in the file is read into the
'uncompacted' struct in the program, some of the data will actually
fall in the invisible gaps (padding) basically corrupting the data
while it's being read.
Out of interest, why are you poking around with this? Something that
caught your interest, or is it something that's causing you a problem?
(Before you started messing with the pragma that is ;) )
--
PJH
http://shabbleland.myminicity.com/ind