(学习路径http://blog.csdn.NET/lanouluanbin/article/details/53518018)
18内存管理
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
/*
1,栈区 系统控制分配和回收 函数中定义的变量都是在栈区
2,**堆区 由程序员控制开辟内存和回收内存
3,常量区
4,静态区 静态区中保存的内容不会消失 之后当程序完全退出之后才会消失
5,代码区 存放源代码的
*/
for (int i = 0; i<5; i++) {
int a = 10;
a++;
printf("a = %d\n",a);
}
for (int i = 0; i<5; i++) {
//静态变量只初始化一次 在程序编译过程中
static int a = 10;
a++;
printf("%d\n",a);
}
//在堆区开辟内存空间
int *p = malloc(4);
*p = 100;
//在堆内存中开辟一块空间 来保存4个整数
int *p1 = malloc(16);
*p1 = 10;
*(p1+1) = 20;
*(p1+2) = 30;
*(p1+3) = 40;
//回收内存(告诉编译器这块内存我不用了,你可以给其它人用了)
free(p1);
//内存泄露(有内存无法回收)
int *p2 = NULL;
p2 = malloc(4);//这四个字节没有回收
free(p2);
p2 = malloc(8);
*p2 = 1000;
free(p2);
//野指针
int *p3 = malloc(10);
*p3 = 10000;
free(p3);
*p3 = 5000;//言而无信 你说不用了 又来用
NSCountedSet *set = [[NSCountedSet alloc] init];
for (int i = 0; i<88888888; i++) {
//1-33
int number = arc4random()%33+1;
NSString *str = [NSString stringWithFormat:@"%d",number];
[set addObject:str];
}
for (int i = 1; i<34; i++) {
NSString *str = [NSString stringWithFormat:@"%d",i];
NSLog(@"%d---->>>%ld",i,[set countForObject:str]);
}
return 0;
}