Write the memory alignment for cross-platform code

Source: Internet
Author: User

When you write a network packet (stored on a heap) translator, you encounter the si_code:1-bus_adraln-invalid address alignment when running on the HP-UX machine. Refer to the following link this helps in handling unaligned data error, was cleared due to the HP-UX CPU based IA-64 architecture, does not support memory random access, need to do byte-order alignment Operation.

When the memory data type is cast, the random-read field1 address is most likely not on a single readable address (for example, the address is 0x0001-0x0005), at which point a bus_adraln error is thrown, resulting in an outage.

Such as:

struct shead{uint32_t field1;uint32_t field2;}; Shead *head = (shead*) buf;printf ("%d\n", head->field1);

One solution is to have a memory copy more than once, such as:

struct shead{uint32_t field1;uint32_t field2;}; Shead head;memcpy (&head, buf, sizeof (shead));p rintf ("%d\n", head.field1);

  

An introduction to Memory byte alignment on MSDN:

Many CPUs, such as those based on Alpha, IA-64, MIPS, and SuperH architectures, refuse to read misaligned data. When a program requests that one of the these CPUs access data which is not aligned, the CPU enters an exception state and Noti Fies the software that it cannot continue. On ARM, MIPS, and SH-device platforms, for example, the operating system-default is-to-give the application an exception n Otification when a misaligned access is requested.

Misaligned memory accesses can incur enormous performance losses on targets that does not support them in hardware.

Alignment is a property of a memory address, expressed as the numeric address modulo a power of 2. For example, the address 0x0001103f modulo 4 is 3; That address was said to being aligned to 4n+3, where 4 indicates the chosen power of 2. The alignment of an address depends on the chosen power of. The same address modulo 8 is 7.

An address was said to being aligned to X if it alignment is xn+0.

For the memory alignment is not very familiar with the classmate please refer to:

http://msdn.microsoft.com/en-us/library/ms253949 (vs.80). aspx

Write the memory alignment for cross-platform code

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.