当前位置:网站首页>Arrays and Structures
Arrays and Structures
2022-07-30 03:59:00 【Chicken Island~】
一.数组
提出问题:
- Why should the first element of the array index from0开始?
- Multiple sequence of storage for variables local to is自下而上,还是自下而上分配的呢?
- Why the compiler to allocate12个4Bytes of memory?
猜测1: 用Against the conventionalMethods for计算速度
猜测2:The same way with arguments stack,First statement in the above
第i个元素的地址 = 首地址a + (i-1)×元素大小
第i个元素的地址 = 首地址a + i×元素大小
实证1:
C++代码
int main()
{
int arr[10]{
0};
for (int i = 0; i < 10; i++) {
arr[i] = i;
}
}
汇编代码
003C1048 8B 55 D0 mov edx,dword ptr [ebp-30h]
003C104B 8B 45 D0 mov eax,dword ptr [ebp-30h]
003C104E 89 44 95 D4 mov dword ptr [ebp+edx*4-2Ch],eax
实证2:First statement, first pressure stack,猜测2是错的

发现规律
- CArray of language in the global area,On the stack分配连续的内存空间;而且索引从零开始;
- The local variable memory layout isFirst statement, first pressure stack,The need in the future to further modify the validation;
一.结构体
提出问题:
- 编译器是把Person p当作一个整体还是单独的变量To allocate memory?答:整体
- 为什么结构体Person的内存大小占四个字节?What's the use of redundant one byte?
- Person p{}加了**{}为什么会影响局部变量的内存地址**呢
C++代码
struct Person {
char c;
short a;
};
int main()
{
Person p;
p.c = 0xffff;
p.a = 0xffff;
}
00891004 C6 45 FC FF mov byte ptr [ebp-4],0FFh
00891008 83 C8 FF or eax,0FFFFFFFFh
0089100B 66 89 45 FE mov word ptr [ebp-2],ax

设计实验:From the table we can find the following pattern
- 当对齐字节Than the largest footprint bytes of the structure that a成员变量大时,aThe offset will not change again
- When the byte alignment for1时,The size of the structure will not change,没有字节填充

实验二:No matter how much byte alignment is,All the phenomenon of byte filling won't occur
struct Person {
char a;
char b;
short c;
};
实验三:
struct Person {
char a;
char b;
int c;
};

Speculation and infer(Based on the experimental phenomena):Structure of the data storage to follow the following process
> First select a box,Then in order to field put into box,When the box can not let go,Again with another box to store,Until all fields are stored to complete.
> 盒子长度= min{
max{
sizeof(成员变量) }, 对齐长度 }
> Variable in the rules of the box:The head offset bytes from the box= n×sizeof(成员变量) (n=0,1,2,…)
实证:
struct Person {
char a;
short a2;
char b;
int c;
};
实际应用
待填充
边栏推荐
- redis分布式锁的原子保证
- WeChat second-hand transaction small program graduation design finished works (8) graduation design thesis template
- Pytorch framework learning record 1 - Dataset class code combat
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (7) Interim Inspection Report
- ospf 导图
- (redistribute, special comprehensive experiment ospf area)
- spicy (two) unit hooks
- 小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书
- spicy (1) basic definition
- Eureka注册中心
猜你喜欢

组织在线化:组织数字化变革的新趋势

(redistribute, special comprehensive experiment ospf area)

Pytorch框架学习记录7——卷积层

小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书

Basic introduction to protect the network operations

为什么突然间麒麟 9000 5G 版本,又有库存了?

Pytorch framework learning record 3 - the use of Transform

护网行动基本介绍

Redis "super explanation!!!!!!"

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (4) Opening Report
随机推荐
Nacos cluster partition
国内首家沉浸式高逼真元宇宙,希元宇宙正式上线
护网行动基本介绍
Tcp编程
Send it to your friends and let TA treat you to fried chicken!
进程优先级 nice
Nacos installation and deployment
Small application project works WeChat integral mall small program of graduation design (4) the opening report of finished product
函数的底层机制
vscode debugging and remote
Microservice CAP Principles
小程序毕设作品之微信积分商城小程序毕业设计成品(5)任务书
运行时间监控:如何确保网络设备运行时间
传统项目转型
一直空、一直爽,继续抄顶告捷!
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!
对均匀采样信号进行重采样
新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (7) Interim Inspection Report