惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - ProjectDD

NET 中 Async/Await 的演进:从状态机到运行时优化的 Continuation C# 指针用法小结 C# BinaryPrimitives 类 linq 查询关于 from子句 关于C# await的一点新理解 关于排序算法 C# Dev Kit 经常导致崩溃 不太会用Span<T> 看文档上的优点估摸着试试 span,memory,ArrayPool,MemoryPool,等的性能对比 C# simd 性能雷点记录 C# 模式匹配里应该注意的几点 高中生理解梯度为何是方向导数极大值 概略 deep net 通过relu 进行函数逼近 win10 下安装 rust 的 依赖配置,通过vs2022 C# 有多需要aot 24个希腊字母的中文拼音版 英语发音探讨 sagemath 9.x 下的 jupyter 工作路径设置 Serializing delegates is not supported on this platform
C# 内存对齐
ProjectDD · 2024-10-06 · via 博客园 - ProjectDD

[StructLayout(LayoutKind.Sequential)]    

unsafe struct B{

  // public int field1;

  public double field2;

  public string field3;

  byte* field4;

  byte field5;

  nint field6;

  byte field7;

  byte field8;

  short field9;

  short field10;

  short field11;

  // decimal field7;

}

 sizeof(B)=48  ;

和LayoutKind.Auto的结果一样的,  反复试了几遍感觉似乎和字段的顺序没关系,打开field1 的话,它还是会和别的不足一个pack的字段接合。。 除非出现转弯

那么我来名词解释下,pack 就是说内存地址的长度,64位=8字节在这样的内存平台上 pack=address length=8 byte , 在32位内存平台上,是4 byte , C#内存对齐的规则 大致是这样的,从office 0开始往后依次排,按address len来计算,只要多个字段的长度相加没出现“转弯” 或 称为“折叠”那么这种 多个字段放入一个 address len(pack)之内的情况就是会发生的,应该是自动发生的,比方一个 byte 类型是 1,一个short 是2,一个 int是4 这些字段都小于 memory address len  所以多个这样的字段可以放入一个 pack里,并且这是自动的,但是若 一个Int + double这就不行,因为4+8 =12 已经大于一个pack =8了,所以出同现叠这就不允许 (这个规则 是我猜测的,是符合逻辑的推测)因为若出现各种折叠则 让字段的读取和识别将变得异常复杂 和困难

[StructLayout(LayoutKind.Sequential)]
struct C1{
  int filed1;
  double field2;
  short field3;
}
[StructLayout(LayoutKind.Auto)]
struct C2{
  int filed1;
  double field2;
  short field3;
}
[StructLayout(LayoutKind.Sequential)]
struct C3{
  int filed1;
  short field3;
  double field2;
}
[StructLayout(LayoutKind.Auto)]
struct C4{
  int filed1;
  short field3;
  double field2;
}

unsafe{
  Console.WriteLine("Sequential,C1:int,double,short sizeof:{0}",sizeof(C1));
  Console.WriteLine("Auto,C2:int,double,short sizeof:{0}",sizeof(C2));
  Console.WriteLine("Sequential,C3:int,short,double sizeof:{0}",sizeof(C3));
  Console.WriteLine("Auto,C4:int,short,double sizeof:{0}",sizeof(C4));
}

Sequential,C1:int,double,short sizeof:24
Auto,C2:int,double,short sizeof:16
Sequential,C3:int,short,double sizeof:16
Auto,C4:int,short,double sizeof:16

using System.Runtime.InteropServices;
unsafe struct A{
  public int field1;
  public double field2;
  public string field3;
  public int Field1 { get => field1; set => field1 = value; }
  public double Field2 { get => field2; set => field2 = value; }
  public void M1() => Console.WriteLine("M1");
}
[StructLayout(LayoutKind.Sequential)]    
unsafe struct B{
  public int field1;
  public double field2;
  public string field3;
  byte* field4;
  byte field5;
  nint field6;
  byte field7;
  byte field8;
  short field9;
  short field10;
  short field11;
  // decimal field7;
}
[StructLayout(LayoutKind.Auto)]    
unsafe struct B2{
  public int field1;
  public double field2;
  public string field3;
  byte* field4;
  byte field5;
  nint field6;
  byte field7;
  byte field8;
  short field9;
  short field10;
  short field11;
  // decimal field7;
}

unsafe{
  var x1=new A();
  var p1 = &x1;
  p1->Field1=10;
  Console.WriteLine(x1.Field1);
  p1->M1();
  Console.WriteLine(sizeof(A));
  Console.WriteLine(sizeof(B));
  Console.WriteLine(sizeof(B2));
}

10
M1
24
48
48

为什么 sizeof(B) 和sizeof(B2) 都为48 没搞懂,不应该说 sizeof(B) = 56吗? 谁能解释