本文介绍了函数调用中的对象引用相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 -(UserDetail *)functionCheck :(NSString *)str
 {
    UserDetail *d2=[[UserDetail alloc] init];
    NSLog(@"check address::::::> %p",&d2);
    d2.auth_token=str;
    return  d2;

 }

每当我调用functionCheck()函数时,都会打印相同的地址

whenever i am calling functionCheck() function print same address

检查地址::::::> 0xbfffd500

check address::::::> 0xbfffd500

表示分配相同的地址.返回后如何取消分配UserDetail d2对象.

means it allocate same address.how to deallocate UserDetail d2 object after return.

推荐答案

d2是分配对象的指针,所以您要记录值d2中,不是其地址 &d2:

d2 is a pointer to the allocated object, so what you want is to log the value of d2, not its address &d2:

NSLog(@"check address::::::> %p", d2); // remove the & !

(&d2是本地堆栈变量的地址,每次调用可能相同.)

(&d2 is the address of the local stack variable, and that may be the same for each call.)

这篇关于函数调用中的对象引用相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 16:58