objective-c - 为什么指向用户定义对象的指针在内存中与用户定义对象本身的地址不同
问题描述
我在学习objective-c,我想从objective-c的角度来理解指针背后的概念。鉴于下面的代码,我希望 3 语句显示相同的结果或至少显示 sam 内存地址。至少因为,指针
meAsImpl refers/points/observs the memory location of the user-defined object MeAsImpl
Hence, the 3 NSLog statement should display the same results
代码的输出如下:
<MeAsImpl: 0x600000194120>
<MeAsImpl: 0x60000019f240>
<MeAsImpl: 0x60000019f240>
请让我知道为什么第一个值与后面的两个值不同
代码:
MeAsImpl *meAsImpl = [[MeAsImpl alloc] init];
NSLog(@"%@", meAsImpl);
NSLog(@"%@", MeAsImpl.alloc);
NSLog(@"%@", MeAsImpl.alloc.init);
解决方案
每次调用时,都会alloc
为调用者分配新的内存地址。init
刚刚初始化了已经分配的内存,所以地址不会改变init
你使用alloc
两次,你得到两个内存地址。
推荐阅读
- logging - Elixir:如何将记录器连接到特定的记录器后端
- paperjs - 缩放和平移修复
- c++ - 为什么不转发参考常量?
- javascript - 如何使用 js 解析 XMLHttpRequest 的 HTML 响应文本?
- python - 计算每行的“最好的 4”平均值
- python - 检查列表中连续 X 个相同元素
- php - 在画廊中显示一些带有日期的图像
- c# - 将 Entity Framework Core 实体转换为 SQL 字符串
- wordpress - 获取媒体中的 ACF 自定义字段 - Twig/Timber
- git - 为什么管道会从 git log 中删除分支名称?