ANaiveProgrammer <my*******@gmail.com> wrote:
Many thanks for your reply...
The size of ef is sizeof(ef), not 10 or any other explicit integer.
The layout of structures varies from implementation to implementation,
so there is no guarantee that the sending and receiving machine
agree on the size, byte order, alignment, padding, etc. of a raw
binary structure.
So bottom line is , my structurs will work?????????am i right.....
Where did you got that from Gordons reply (and, please, keep proper
attributions)?
what is padding in structures, why do compilers do that ? i mean im
stupmed about this....wht is the advantange of using Byte padding in
structures....
The advantage of padding bytes is that the compiler can produce
code that won't crash with bus errors on some machines. There are
quite a number of machines where accessing e.g. odd addresses is
not allowed. So, when you have e.g. a structure like
struct crash_me {
int a;
char b;
int c;
} x;
and sizeof(int) is at an even and sizeof(char) an odd number, then,
without an additional padding byte, 'c' would be at an odd address
(the compiler makes sure that the structure does not start at an
odd address in such cases). And the result would be that for
x.c = 3;
the computer would have to write a value to an int that is at an odd
address and that would kill the program. In order to avoid that the
compiler automatically inserts at least one "padding byte" between
the members 'b' and 'c' of the structure, so that accessing the member
'c' won't lead to the program getting aborted.
You are also for example not allowed to do things like
int a;
*( ( char * ) a + 1 ) = 1;
because that could result in an "unaligned" access, i.e., in the example
from above, in an access to an odd address.
That's probably the main reason for padding bytes. But even on machines
where accesses to odd addresses are allowed it's often a lot slower
than accessing even addresses (e.g. i386 CPUs).
Regards, Jens
--
\ Jens Thoms Toerring ___
Je***********@physik.fu-berlin.de
\__________________________
http://www.toerring.de