Structure byte alignment / Renesas CA78K0 compiler structure alignment rules

Structure byte alignment
1. In Renesas CA78K0R compiler The
default structure in Renesas CA78K0R compiler is aligned with 2 bytes. Change the compiler setting Compile Options -> Data Control -> Pack Structure Members to Yes (-rc), as shown in Figure 1. It can be adjusted to 1-byte alignment, that is, the structure is continuous and there is no padding area. The proof can be found in the IDE's help, as shown in Figure 2. However, changing this setting only takes effect on the structure, and has no effect on other variable types.
Figure 1 Renesas compiler option settings Compiler settings
Figure 2 Description of structure alignment in helpFigure 1 Description in help

实测如下:	
1. 当没有关闭内存对齐时:定义结构体
	(1)如果只有一个u8类型的变量,不会填充;
	(2)当定义大于1个的奇数个u8类型的变量,编译器会填充剩下的一个 字节区域,保证内存大小是2的倍数;
	(3)定义其他类型情况如上第2条。
2. 关闭内存对齐之后
	使用sizeof()函数测得结构体的大小即为各个成员大小之和,不会出现填充区。但是内存访问结构体的速度会下降。

2. Regarding the byte alignment of the
structure memory The structure has byte alignment to improve the efficiency of access. Generally, the following three rules are followed (available by checking the information):
(1) The first address of the structure variable can be maximized by it. Wide basic type members are divisible by the size;
(2) The offset of each member of the structure relative to the first address of the structure (offset) is an integer multiple of the member size, if necessary, the compiler will add between the members Internal adding;
(3) The total size of the structure is an integer multiple of the size of the widest basic type member of the structure. If necessary, the compiler will add trailing padding after the last member. .
According to the above rules, the size of the widest basic type when accessing the basic unit of the structure member can be inferred. For example, the maximum width of the member type is 4 bytes, and the size of the structure is a multiple of 4. Therefore, it is necessary to arrange the order of members according to the above rules to reduce the appearance of unnecessary filling areas and improve the utilization of memory. Of course, the number of byte alignment can be set by the programmer himself.
Reference materials:

  1. https://www.cnblogs.com/luxiaoxun/archive/2012/11/09/2762438.html
  2. https://mp.weixin.qq.com/s?src=11&timestamp=1584668527&ver=2227&signature=fauI0AXWyr0BSdH45DDXaKlGaZoUXH3UgTf8oHmA3ZpUrVKnbtNIyemqmPiMfSVKzXjxeOA1tQvqXLYIutQLw0P-dUIc8yc3KjVnZ63yIUVD5LEGxxam-BwsGSxocT2y&new=1

Guess you like

Origin blog.csdn.net/qq_36439722/article/details/105071682