当前位置:网站首页>C memory alignment
C memory alignment
2022-06-11 07:26:00 【Nwafu_ zyccc】
Reference resources :https://www.cnblogs.com/sunbines/p/9257981.html
Improve the specific graphical display of memory alignment !
Alignment reason :
1) Platform reasons ( Reasons for transplantation ): Not all hardware platforms can access any data at any address , Some hardware platforms can only access certain types of data at certain addresses , Otherwise, a hardware exception will be thrown
2) Hardware reasons : After memory alignment ,CPU The memory access speed of is greatly improved .
- Alignment principle :
【 principle 1】 Data member alignment rules : structure (struct)( Or in association with (union)) Data members of , The first data member is placed in offset by 0 The place of , In the future, the alignment of each data member is in accordance with #pragma pack The specified value and the length of the data member itself , The smaller one is going to .
【 principle 2】 structure ( Or in association with ) The overall alignment rule of : After the data members are aligned , structure ( Or in association with ) Alignment itself , The alignment will follow #pragma pack Specified values and structures ( Or in association with ) In the maximum data member length , The smaller one is going to .
【 principle 3】 The structure as a member : If there are some members in a structure , Structure members are stored from the integer multiple address of the maximum element size inside .
remarks : Array members are calculated by the length of the array type , Such as char t[9], In the 1 In the step, the length of the data itself is determined by 1 count , When accumulating structures, the length is 9; The first 2 In step , When finding the maximum data length , If the structure T There are complex type members A, The A The length of the member is the member of the complex type A Maximum member length of .
Natural alignment : The address where the variable is stored is an integer multiple of the data type size of the variable . Such as : Deposit int The address of type data must be 4 Multiple , Deposit short The address of type data must be 2 Multiple .
Change the default boundary condition ( Specify the opposite boundary ):
Using pseudo instructions #pragma pack (n),C The compiler will follow n Byte alignment .
Using pseudo instructions #pragma pack(), Cancel custom byte alignment .
#include <stdio.h>
#include "io_utils.h"
typedef struct Person{
char *name;
int age;
short s;
double salary;
}Person;
int main() {
Person *person ;
person->name = "zyccc";
person->age = 22;
person->s = 2;
person->salary = 998999;
PRINTLNF("2333");
return 0;
}

As shown in the figure, it can be concluded that front char* Is a pointer type my OS yes 64 Location Account for the 8 Bytes 【0-5】 And then because of int The type is 4 Bytes So the address alignment should be 4 A multiple therefore The first 【6-7】 A byte complements 0 Align memory Back short Types occupy 2 Bytes 【12-13】 because double Types occupy 8 Bytes need to be completed 0 To the first 16 Location therefore Mended 6 individual 0 The following is the corresponding eight bytes double For the corresponding 【16-24】. *** double The number of bytes occupied is related to the actual size as well as float

And the following outputs can be clearly drawn The above conclusion
The code is as follows
#include <stdio.h>
#include "io_utils.h"
#include <stddef.h>
typedef struct Person{
char *name;
int age;
short s;
double salary;
}Person;
int main() {
Person *person ;
person->name = "zyccc";
person->age = 22;
person->s = 2;
person->salary = 9989999;
PRINT_INT(_Alignof(person->age));
PRINT_INT(offsetof(Person ,age));
PRINT_INT(sizeof(Person));
return 0;
}
边栏推荐
- gaussDB for redis和 redis的区别?
- Library management system 2- demand analysis
- I/o multiplexing - select/poll/epoll
- es5和es6的学习小记
- 1734. arrangement after decoding XOR
- 软件测试周刊(第75期):唯有平视,才能看见真实的自己。
- nosqlzoo刷题-1
- The rotation of the earth and the moon (II)
- P3811 [template] multiplicative inverse
- Mobile console Gobang (first draft of detailed design)
猜你喜欢

学 SQL 必须了解的10个高级概念

2022.5.30-6.5 AI行业周刊(第100期):三年时光

No response from win10 explorer when dragging files

Leetcode-141. Linked List Cycle

2、 User login and registration

Installation de SQL Server 2008 (avec mot de passe), création d'une base de données, test de projet de formulaire C

一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试

JVM tuning

Outer margin collapse

教育专家王中泽老师一招解决学生问题
随机推荐
Analyse du contrat du modèle de taux composé
1734. arrangement after decoding XOR
Leetcode-647. Palindromic Substrings
农房一体脚本的心得记录
[Oracle database] mammy tutorial day04 Sorting Query
软件测试周刊(第75期):唯有平视,才能看见真实的自己。
MS office level II wrong question record [6]
[analysis of STL source code] summary notes (6): Design of iterator and magical traits
[analysis of STL source code] summary note (4): behind the scenes hero allocator
学 SQL 必须了解的10个高级概念
pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
[STL source code analysis] summary notes (7): ingenious deque
CMAP of Matplotlib
Leetcode-104. Maximum Depth of Binary Tree
The maximum number of divisors of numbers in the int range is 1536
MS office level II wrong question record [4]
【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用
P3172 [cqoi2015] data selection (Mobius inversion + Du Jiao sieve)
自动化测试的生命周期是什么?
Android and IOS reverse analysis / security detection / penetration testing framework