Hi,
What is the concept of memory alignment?
The mechanism of memory alignment is not defined by the C language
standard, the standard merely allows an implementation to require it
and states that a program violating it causes undefined behavior.
In almost all cases, alignment is used to place an object of n-bytes
on an address that is either a multiple of n or a multiple of the
memory bus width of the processor, whichever is less.
Some processors dedicate extra circuitry to handling data that is not
properly aligned. Consider a processor with 8-bit bytes where type
int has 32 bits and the processor has a 32-bit path to memory. If an
int is located at address divisible by 4, the processor can read or
write all 32 bits in a single memory cycle. If the address is not
divisible by 4, it would have to perform two reads or writes to put
part of the int into two different 32-bit blocks. This causes the
program to execute more slowly, in some cases drastically so.
Other processors do not dedicate circuitry to automatically handle
misaligned data. Some ignore the lowest bits of the address and
therefore read or write something other than what the programmer
intended. Others generate hardware traps when a misaligned memory
access happens, that typically causes the operating system to shut
down the program.
Is memory alignment differs, If a data type is local to a function or
if it is a member of structure or union?
The C standard does not define this, but most likely these factors
make no difference.
How 32 to 64 bit processor afftects the memory alignment?
It doesn't have anything in particular to do with the word size of the
processor. It has to do with the issues above, which are largely
independent of word size.