Skip to content

Instantly share code, notes, and snippets.

@623637646
Created May 7, 2020 06:24
Show Gist options
  • Save 623637646/8a9c4a67b1632624f2bcc6a33db78fdc to your computer and use it in GitHub Desktop.
Save 623637646/8a9c4a67b1632624f2bcc6a33db78fdc to your computer and use it in GitHub Desktop.
Swift中,一个类实例的内存布局是有规律的.
Swift中,一个类实例的内存布局是有规律的:
1. 32位机器上,类前面有4+8个字节存储meta信息,64位机器上,有8+8个字节;
2. 内存中,字段从前往后有序排列;
3. 如果该类继承自某一个类,那么父类的字段在前;
4. Optional会增加一个字节来存储.None/.Some信息;
5. 每个字段需要考虑内存对齐;
这方面尚未从官方的资料找到参考,上述规律一些是从网上其他大神的总结中收集,一些从Clang的一些说明文档中挖掘,加上自己的反复验证得到。
Refer to: https://www.jianshu.com/p/eac4a92b44ef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment