内存管理4Aotorelease自动引用计数

简介:

Student.h:

#import <Foundation/Foundation.h>  @interface Student : NSObject @property(nonatomic,unsigned)int age; +(id)student; +(id)initWithAge:(int)age; @end

Student.m:

#import "Student.h"  @implementation Student  +(id)student{     return [[[Student alloc] init] autorelease]; }  +(id)initWithAge:(int)age{     Student *stu=[[[Student alloc] init] autorelease];     stu.age=age;     return stu; }  -(void)dealloc{     NSLog(@"%@被销毁",self);     [super dealloc]; } @end

main:

#import <Foundation/Foundation.h> #import "Student.h"  int main(int argc, const char * argv[]) {     //@autoreleasepool代表自动创建一个自动释放池     @autoreleasepool {                  Student * stu=[[Student alloc] init];         [stu autorelease];          Student * stu1=[[Student alloc] init];         [stu1 autorelease];          //快速创建Student对象         Student * stu3=[[Student student] autorelease];     } //    @autoreleasepool { // //        Student * stu2=[[[Student alloc] init] autorelease]; ////        [stu2 autorelease]; // //    }     return 0; }























本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366774,如需转载请自行联系原作者


相关文章
|
3月前
|
存储 C语言 C++
C++:C/C++内存管理
C++:C/C++内存管理
38 0
|
7月前
|
存储 C语言 C++
【C++】内存管理
【C++】内存管理
32 0
|
3天前
|
存储 编译器 C语言
C++中的内存管理
C++中的内存管理
15 0
|
2月前
|
存储 算法 C语言
C++内存管理
C++内存管理
24 0
|
3月前
|
存储 C语言 C++
【c++】C/C++内存管理
【c++】C/C++内存管理
【c++】C/C++内存管理
|
9月前
|
C语言 C++
C/C++内存管理
C/C++内存管理
44 0
|
5月前
|
C语言 C++ Windows
【C++】:内存管理
【C++】:内存管理
35 0
|
5月前
|
存储 Java C语言
c++内存管理
c++内存管理
39 0
|
7月前
|
存储 程序员 C语言
C 内存管理
C 内存管理。
23 0
|
11月前
|
存储 C语言 C++
【C++】内存管理(上)
【C++】内存管理
48 0