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;
}