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

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - 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吗? 谁能解释