当前位置:网站首页>一套关于 内存对齐 的C#面试题,做错的人很多!
一套关于 内存对齐 的C#面试题,做错的人很多!
2022-04-23 03:02:00 【dotNET跨平台】
这是一套朋友公司的面试题,挺有意思分享一下。
题目:判断下面的 Location1 和 Location2 的结构体大小各是多少?
public struct Location1
{
public int X;
public int Y;
public long Z;
}
public struct Location2
{
public int X;
public long Y;
public int Z;
}
据反馈 90% 的人说一样大,毕竟从代码看仅仅做了一次 Y 和 Z 顺序的交换,那真的是这样吗?可以用 windbg 调试下就好了。
完整代码代码如下:
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Location1 location1;
Location2 location2;
location1.X = 10;
location1.Y = 11;
location1.Z = 12;
location2.X = 10;
location2.Y = 11;
location2.Z = 12;
Debugger.Break();
}
}
public struct Location1
{
public int X;
public int Y;
public long Z;
}
public struct Location2
{
public int X;
public long Z;
public int Y;
}
}
windbg 输出结果如下:
0:000> !clrstack -a
OS Thread Id: 0x1750 (0)
Child SP IP Call Site
00000000007fef68 00007ff9a84b9ad2 [HelperMethodFrame: 00000000007fef68] System.Diagnostics.Debugger.BreakInternal()
00000000007ff050 00007ff989c0f5ee System.Diagnostics.Debugger.Break()
00000000007ff0a0 00007ff92db5090b ConsoleApp2.Program.Main(System.String[]) [D:\net5\ConsoleApp4\ConsoleApp2\Program.cs @ 26]
PARAMETERS:
args (0x00000000007ff110) = 0x0000000003492cf0
LOCALS:
0x00000000007ff0e0 = 0x0000000b0000000a
0x00000000007ff0c8 = 0x000000000000000a
00000000007ff2f8 00007ff98d086913 [GCFrame: 00000000007ff2f8]
0:000> dp 0x00000000007ff0c8
00000000`007ff0c8 00000000`0000000a 00000000`0000000c
00000000`007ff0d8 00000000`0000000b 0000000b`0000000a
00000000`007ff0e8 00000000`0000000c 00000000`007ff1f8
由于 栈 是从大到小生长的,所以用 dp 命令的时候, location2 是排在 location1 的前方,可以清楚的看到
location2: 000000000000000a 000000000000000c 000000000000000b很明显它的size=3*8=24byte。location1: 0000000b0000000a 000000000000000c它的size=2*8=16byte
那为什么会差 8byte 呢?如果有熟悉 C/C++ 的朋友这时候应该知道,其实就是 内存对齐 ,为什么会出现 内存对齐 ?我们知道,内存是按照 byte 编址的,也就是一个地址存放一个byte,但cpu可不是这么玩的,它的一次读取数是根据 地址总线 来的,目前我们 cpu 基本都是 64根数据总线,也就是一次性可以读取 8个byte。
为了能够让 cpu 读取效率更高,编译器会适当的进行 padding 操作,目的就是 按8 对齐,如果不对齐的话,cpu可能就会出现读不全,也就导致必须至少两次才能读取完毕的情况,肯定会影响 cpu 效率的,还有一个原因是:有些机器必须对齐访问,否则就会异常,所以编译器为了更好的平台移植性,只能对齐啦!
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124262952
边栏推荐
- C#中切片语法糖的使用
- 基于ele封装下拉菜单等组件
- How to use C language to realize [guessing numbers game]
- AC380V drop 5v12v24v200ma, UHV non isolated chip IC scheme
- Cherno_ Game engine series tutorial (5): 101~
- Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
- C#中元组对象Tuple的使用
- 《信息系统项目管理师总结》第七章 项目沟通管理
- Sonic cloud real machine tutorial
- Numpy append function
猜你喜欢

Onenet connection process

Plug in for vscode

Blazor University (12)组件 — 组件生命周期

Summary of software test interview questions

L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
![FileNotFoundError: [Errno 2] No such file or directory](/img/ea/0c3f2768d14c1f4bb42bd1309ab996.png)
FileNotFoundError: [Errno 2] No such file or directory

Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)

Shell script learning notes - regular expressions

Response processing of openfeign

AspNetCore配置多环境log4net配置文件
随机推荐
Onenet connection process
Introduction and use of openfeign component
对.NET未来的一点感悟
tf. keras. layers. Embedding function
If MySQL / SQL server judges that the table or temporary table exists, it will be deleted
Log cutting - build a remote log collection server
Encapsulation of ele table
JS learning notes
Table space capacity query and expansion of Oracle Database
Restart redis
Chapter IV project cost management of information system project manager summary
Leangoo brain map - shared multi person collaborative mind mapping tool
Regular object type conversion tool - Common DOM class
Wepy learning record
The usage of case when and select case when is very easy to use
How to write the expected salary on your resume to double your salary during the interview?
JS using the parameters of art template
Service avalanche effect
Q-Learning & Sarsa
Dynamic sequence table + OJ