简体   繁体   English

复制时保留计数

[英]Retain count while copying

I am quiet new with memory management. 我对内存管理很陌生。 I know my question has already been discussed on StackOverflow. 我知道我的问题已经在StackOverflow上讨论过了。 But I would like to know the answer to understand it completely. 但我想知道答案才能完全理解它。 My question is: 我的问题是:

NSMutableArray *firstArray = [[NSMutableArray alloc]init];
NSMutableArray *secondArray = [[NSMutableArray alloc] init];
secondArray = [firstArray copy];

what will be retain count for firstArray and the secondArray after copy? 复制后firstArray和secondArray的保留计数是多少?

1 for each. 每个1个。 firstArray is holding the pointer to firstArray . firstArray持有指向firstArray的指针。 When you call secondArray = [firstArray copy]; 当您调用secondArray = [firstArray copy]; , the contents of firstArray are copied to a new memory location and secondArray will point to that location. ,将firstArray的内容复制到新的内存位置, secondArray将指向该位置。 This means that firstArray and secondArray will be pointing to separate memory locations (and are different objects), although they have the same data. 这意味着, firstArraysecondArray具有相同的数据,但它们将指向单独的内存位置(并且是不同的对象)。 Therefore, they each have a retain count of 1. 因此,它们各自的保留计数为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM