当前位置:网站首页>一套关于 内存对齐 的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
边栏推荐
- L2-006 树的遍历(中后序确定二叉树&层序遍历)
- C# 11 的这个新特性,我愿称之最强!
- 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
- Typescript Learning Guide
- Table space capacity query and expansion of Oracle Database
- 基于多态的职工管理系统源码与一些理解
- Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
- VirtualBox virtual machine (Oracle VM)
- Onenet connection process
- The express project changes the jade template to art template
猜你喜欢

tf. keras. layers. Conv? D function

Shell script learning notes - regular expressions

Source code and some understanding of employee management system based on polymorphism

The way to conquer C language

Summary of interface automation interview questions for software testing
![[software testing] understand the basic knowledge of software testing](/img/ff/8fcd4b88de28505989aaf517d16113.png)
[software testing] understand the basic knowledge of software testing

Summary of software test interview questions

Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署

C#中切片语法糖的使用

.NET点滴:说说Middleware构造中获取不到Scoped服务的问题
随机推荐
Introduction to ACM [TSP problem]
ele之Table表格的封装
对.NET未来的一点感悟
Numpy stack function
The way to conquer C language
Xamarin效果第二十二篇之录音效果
Innovation and management based on Scrum
JS using the parameters of art template
Use of MySQL command line client and common commands
@Usage and difference between mapper and @ repository
Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
Classification of technology selection (2022)
Sonic cloud real machine tutorial
Dynamic sequence table + OJ
JS learning notes
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
Er and eer models
Winsock programming interface experiment: implementation of ipconfig
Reverse a linked list < difficulty coefficient >