> What is mean by memory alignment in context of structure padding?
Certain C types need to be aligned so that their addresses fall on
a multiple of some number (often, but not always, their size) for
hardware (you get an exception if you don't follow this rule) or
efficiency (you might have to do two memory fetches instead of one)
reasons. For example, 2-byte integers might need to be aligned on
a multiple of 2, and 4-byte integers or pointers might need to be aligned
on a multiple of 2 or 4, and 8-byte integers or doubles might need
to be aligned on a multiple of 2, 4, or 8.
also why structure padding is useful?
It makes the program run (as opposed to throwing smegmentation
faults or some other kind of alignment error trap), or run faster,
on the hardware you are using.
is it implicitly done by latest
c,c++ compilers or i have to specify it explicitly? how?
There is no standard way to explicitly specify it. It's not padding
if you specifically add an unused structure member, and doing so
may not do what to you expect to the padding in the next release
of the compiler, or on a different platform.
also why there is differece for data type integer on MS-DOS as 2 bytes
and on Windows as 4 bytes?
Because the compiler writers can.
why the same is not true for short data type?
Because the compiler writers decided not to.
A lot of this has to do with the processor it's running on and how it
is being used. MS-DOS has its roots in 8086 real mode. Windows
uses more of the memory-mapping features of the processor and is
designed around having a lot more memory available.
Gordon L. Burditt