当前位置:网站首页>A set of C interview questions about memory alignment. Many people make mistakes!
A set of C interview questions about memory alignment. Many people make mistakes!
2022-04-23 03:06:00 【Dotnet cross platform】
This is a set of interview questions from a friend's company , It's interesting to share .
subject : Judge the following Location1
and Location2
What is the size of each structure ?
public struct Location1
{
public int X;
public int Y;
public long Z;
}
public struct Location2
{
public int X;
public long Y;
public int Z;
}
According to feedback 90%
People say it's the same size , After all, from the code point of view, it was only done once Y
and Z
Sequential exchange , Is that really the case ? It can be used windbg Just debug .
The complete code is as follows :
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 The output is as follows :
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
because Stack
It grows from big to small , So use dp When ordered , location2 It's in line location1 In front of , You can see it clearly
location2: 000000000000000a 000000000000000c 000000000000000b
It's obvioussize=3*8=24byte
.location1: 0000000b0000000a 000000000000000c
itssize=2*8=16byte
Then why is it bad 8byte
Well ? If you are familiar with C/C++
My friends should know at this time , In fact, that is Memory alignment
, Why does it show up Memory alignment
? We know , Memory is based on byte
Addressable , That is, one address stores one byte, but cpu It's not like that , Its number of reads at a time is based on Address bus
To the , At present, we cpu Basic it is 64 Root data bus
, That is, it can be read at one time 8 individual byte
.
In order to be able to make cpu Read more efficiently , The compiler will do it properly padding operation , The purpose is to Press 8
alignment , If not aligned ,cpu There may be incomplete reading , This leads to the situation that it must be read at least twice , It will definitely affect cpu Efficient , Another reason is : Some machines must be aligned to access , Otherwise, it will be abnormal , So the compiler for better platform portability , Only alignment !
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301334359.html
边栏推荐
- MAUI初体验:爽
- 求二叉树的叶子结点个数
- Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
- Passing object type parameters through openfeign
- HLS / chisel practice CORDIC high performance computing complex square root
- TP5 inherits base and uses the variables in base
- 使用split来解决“最常见的单词”问题
- 樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
- 利用栈的回溯来解决“文件的最长绝对路径”问题
- Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
猜你喜欢
.NET点滴:说说Middleware构造中获取不到Scoped服务的问题
由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
Xamarin效果第二十二篇之录音效果
Introduction to ACM [TSP problem]
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
Depth deterministic strategy gradient (ddpg)
Judge whether there is a leap year in the given year
Ningde's position in the times is not guaranteed?
随机推荐
Publish to NPM?
Use of MySQL command line client and common commands
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
Creating wechat voucher process with PHP
TP5 customization in extend directory succeeded and failed. Return information
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
Vs code setting line feed
Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
Xamarin效果第二十二篇之录音效果
The space between the left and right of the movie ticket seats is empty and cannot be selected
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
If the deep replication of objects is realized through C #?
宁德时代地位不保?
Wepy learning record
利用栈的回溯来解决“文件的最长绝对路径”问题
使用两种方法来解决“最大回文数乘积”问题
Use DFS to solve the problem of "number of dictionary rows"
C# WPF UI框架MahApps切换主题
How to write the expected salary on your resume to double your salary during the interview?