Wednesday, June 8, 2011

Memory alignment

Key points:
1. Each data element is stored at an address which is a multiple of its natural size.
2. Total size of the structure or union will be multiple of the natural size of the biggest data type in the structure or union.


To reduce the memory usage (Sets the alignment of all aggregate members to a specified byte boundary), use
. #pragma pack(1) before the declaration of the structure or union for Windows.
. __attribute__ ((packed)) after the } of the declaration for Linux.


Ex1. Windows.
#pragma pack(1)
struct alignment_test
{
    char x ;
    int y ;
    short z ;
} ;


Ex2. Linux

struct alignment_test
{
    char x ;
    int y ;
    short z ;
} __attribute__ ((packed)) ;

For details:
. http://kezeodsnx.pixnet.net/blog/post/27585076
. http://msdn.microsoft.com/en-us/library/aa505951.aspx
. http://www.ohse.de/uwe/articles/gcc-attributes.html#type-packed
. http://www.aleph1.co.uk/chapter-10-arm-structured-alignment-faq
. http://publib.boulder.ibm.com/infocenter/compbgpl/v9v111/index.jsp?topic=/com.ibm.xlcpp9.bg.doc/compiler_ref/pragma_pack.htm

No comments:

Check clients which connect to Mac OS X Wi-Fi Internet Sharing

arp -i bridge100 -a bridge100 may be different on your Mac OSX