当前位置:网站首页>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;
};
实际应用
待填充
边栏推荐
猜你喜欢

Resampling a uniformly sampled signal

OpenFeign realize load balance

Pytorch framework learning record 3 - the use of Transform

Pytorch framework learning record 5 - the use of DataLoader

小程序毕设作品之微信积分商城小程序毕业设计成品(7)中期检查报告

When the EasyNVR platform is cascaded to the EasyCVR, why can't the video be played after a while?

Rpc 和 gRpc 简介汇总

The difference between BGP room and ordinary room in Beijing

进程优先级 nice

护网行动基本介绍
随机推荐
[Switch] Protocol-Oriented Programming in Swift: Introduction
Organizations Going Online: A New Trend in Organizational Digital Transformation
【驱动】udev为USB转4串口的每个串口起别名
Sentinel Traffic Guard
Send it to your friends and let TA treat you to fried chicken!
MySQ死锁
组织在线化:组织数字化变革的新趋势
弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升
小程序毕设作品之微信二手交易小程序毕业设计成品(6)开题答辩PPT
What is the difference between mission, vision and values?
STM32 SPI+WM8978 voice loopback
vscode debugging and remote
Chapter 51 - Knowing the request header parameter analysis【2022-07-28】
操作配置:如何在一台服务器中以服务方式运行多个EasyCVR程序?
Forum management system
New interface - API interface for "Taote" keyword search
Solve the problem of compiling and installing gdb-10.1 unistd.h:663:3: error: #error “Please include config.h first.”
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (7) Interim Inspection Report
route filter
EasyNVR平台级联到EasyCVR,视频播放一会就无法播放是什么原因?