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

简介: Student.h: #import @interface Student : NSObject@property(nonatomic,unsigned)int age;+(id)student;+(id)initWithAge:(int)age;@end Student.

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;
}


相关文章
|
1月前
|
存储 程序员 编译器
C++:内存管理
C++:内存管理
43 1
|
6月前
|
存储 C语言 C++
|
2月前
|
存储 C语言 C++
【c++】C/C++内存管理
【c++】C/C++内存管理
【c++】C/C++内存管理
|
6月前
|
存储 编译器 C语言
【C++】C/C++内存管理:
1.C中的malloc、realloc、calloc和free函数: 【面试问题】 malloc、calloc和realloc的区别是什么?
51 0
|
8月前
|
C语言 C++
C/C++内存管理
C/C++内存管理
43 0
|
4月前
|
C语言 C++ Windows
【C++】:内存管理
【C++】:内存管理
35 0
|
8月前
|
C++ 容器
C++ 内存管理 基本部分
不能直接调用构造函数,但是可以直接调用析构函数
55 0
|
9月前
|
C语言 C++ Windows
内存管理详解
内存管理详解
38 0
|
10月前
|
存储 编译器 Linux
C&C++内存管理
C&C++内存管理
|
10月前
|
编译器 C语言 C++
【C/C++的内存管理】
前言 本文章将深入探索C/C++的内存管理模式以及区别。 🍕1. C/C++内存分布 C/C++的内存分布有几大块区域:内核空间,栈区,内存映射段,堆区,数据段,代码段等等。

热门文章

最新文章