iOS exc_arm_da_align crashes due to byte alignment

Source: Internet
Author: User

This article was previously linked to: http://www.cnblogs.com/zouzf/p/4455167.html

Let's take a look at this link: http://www.cnblogs.com/ren54/archive/2013/01/11/2856207.html

I encountered the situation and this friend very similar: the binary way from the file to read the content to memory, assuming that the content is only 7 bytes, the front three bytes is three characters, the last four bytes of the content is an int data, when the last four bytes into the int data (pfilecontent is the char* pointer, Point to Fourth byte):int intvalue = * (pfilecontent); It will crash and report exc_arm_da_align errors.

Check the information: http://www.justinyan.me/post/1609, http://www.shaoqun.com/a/54537.aspx, http://blog.csdn.net/slay_cn/ article/details/6221637, is due to the byte alignment problem, presumably described as follows (excerpt from the previous three data):

The memory address space is divided in bytes, so the theoretical access to the memory address can start from any byte, but in fact we do not directly access the hardware address, but rather through the operating system virtual memory address to access, virtual memory address is in Word units. The word length of a 32-bit machine is 32 bits, so a 32-bit machine accesses a memory size that is 4 bytes. Furthermore, for performance reasons, data structures (especially stacks) should be aligned as far as possible on natural boundaries. The reason is that, in order to access unaligned memory, the processor needs to make two memory accesses, while aligned memory access requires only one access. This problem is common in the processing of byte stream parsing, such as receiving a packet from the network, read into the local cache after processing, the first two bytes is a short flag, the next four bytes is an int parameter, so the pointer to this position is directly cast into an int* to read the data-- So the problem arises, when reading int*, ARM asks for byte alignment, which is not aligned at this time. It is possible to crash with a exc_arm_da_align exception.

Presumably, when forcing data type conversions, the (release version) requires memory bytes to be aligned. Compiling the release version, the compiler optimizes the code and requires that the data structure be aligned on the natural boundary to improve CPU efficiency when it is processed.

The workaround is http://www.shaoqun.com/a/54537.aspx:

Scenario 1, when the pointer is defined with the compiler directive packed, the compiler no longer requires byte alignment when encountering this keyword, but does its own proper processing.

Scenario 2, use memcpy byte-by-bit copies to bypass direct int* pointer reads. When the parameter is passed to memcpy, the parameter is converted to the void* type, because the memcpy of release is optimized, and in the optimized version, the pointer is converted to a long type, 4 4 bytes are copied, and then the byte alignment problem occurs.

I used scheme 2 to transfer the memcpy parameters to void*.

Postscript

When reading a file to obtain data is often used in the deserialization of the way to improve efficiency, as mentioned in the previous article on the optimization of skeletal animation, this time to pay special attention to the problem of byte alignment, especially now do hand travel basically involves multi-platform, in defining some of the struct is best to display the call

#pragma pack (n) to specify the byte alignment. Under Windows, the compiler defaults to 8-byte alignment, and Mac, IOS, Android (if wrong) is 4-byte aligned by default, and often occurs when a file resource is serialized under Windows, Then, when running on iOS and Android, read the file for deserialization if there is a struct such as struct myTest {char[5] name; int age;} The problem is that Windows writes into a file that requires 16 bytes, while an arm device requires 12 bytes of Read file deserialization ~ ~

It said: Windows default is 8-byte alignment, Mac, IOS, Android (if wrong) is 4-byte alignment, this argument is not rigorous, it should be more to do with the compiler, and the compiler will be based on the system and CPU characteristics to determine it, this piece of knowledge is not much, Friends who have an understanding can leave links and views, thanks.

There is also a doubt, that exc_arm_da_align collapse will appear in Xcode5.1.1 out of release version, and Xcode5.0.1 release version is Nothing, a small version of the necessary difference is so big ....

This article was previously linked to: http://www.cnblogs.com/zouzf/p/4455167.html

iOS exc_arm_da_align crashes due to byte alignment

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.