Wonderful bug life [2]-unaligned highlights

Source: Internet
Author: User

Speaking of alignment, many students who study C language naturally think of alignment of members in the data structure. For example, they often ask questions during interviews:

Struct {

Char;

Short B;

Int C;

};

How many bytes does it take? This inappropriate example is indeed related to alignment. But how many people are in trouble with alignment?

 

Yesterday, we found a strange phenomenon: the lighting test code is normal, and it is not normal to add a function block. The code block is as follows:

 

Uint8_t pid = 0;

Uint8_t tid = 0;

 

Void foo1 (void)

{

Xxx;

}

 

Void foo2 (void)

{

YYY;

}

 

If you try to comment out foo1 (), it will be normal, and vice versa. I don't know why!

 

In desperation, I picked up openocd + JTAG and found that there was a hard fault exception in the CPU because unaligned occurred. The final reason was: When the startup code was copying data segment data, the Command finds that the data is not aligned, and the CPU exception occurs. The commented-out code just causes the data segment data to be aligned, so there is such a strange phenomenon.

 

As you will know later, when you need to comment out the code by block, the data should also be commented out because it may also cause errors.

This is a piece of cortex-m3 board, the next contact with the new CPU is best to make it and other CPU differences, and then start.

This should be the last interesting bug in my company. I hope my bug will continue to be wonderful.

 

 

Related Article

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.