A byte alignment problem encountered when porting applications to the ARM platform

Source: Internet
Author: User
A byte alignment problem encountered when porting applications to the ARM platform

Example:
Define a class as follows:
Class Example {
Public:
Example ();
PRIVATE:
Unsigned short C;
};

Since unsigned short is two bytes, this class occupies only two bytes on PC,
On the ARM platform, the class is 4 bytes because of the 4-byte alignment problem,
Therefore, when the pointer ++ is used in arm, a problem occurs. :)

The solution is to add the _ attribute _ (packed) parameter to the class when compiling the ARM platform program.

Test code:

# Include <stdio. h>
# Define platform_arm 0

Class Example
{
Public:
Unsigned short C;

# If platform_arm
}__ Attribute _ (packed ));
# Else
};
# Endif

Int main (void)
{
Example * exm = new example;

# If platform_arm
Printf ("On arm/N ");
# Else
Printf ("on PC/N ");
# Endif
Printf ("the sizeof class example is [% d], pointer is [% d]/n"
, Sizeof (example), sizeof (exm ));

Return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.