IOS 階段學習第九天筆記(記憶體管理),ios第九天
IOS學習(C語言)知識點整理
一、記憶體管理
1)malloc , 用於申請記憶體; 結構void *malloc(size_t),需要引用標頭檔<stdlib.h>;在堆裡面申請記憶體,size_t,表示申請空間的大小,單位是位元組;如果申請成功,返回這段記憶體的首地址,申請失敗,返回NULL;申請的記憶體空間需要手動初始化。
注意點:
1、可能會申請失敗,所以需要判斷返回是否是NULL。
2、申請的記憶體需要強制轉換為指定的資料類型,例如:(int*)malloc(10*sizeof(int))
3、分配的記憶體是未初始化的,需要先清理後使用。
4、使用完畢需要手動釋放,如果沒有釋放會導致記憶體流失。
5、什麼時候用:>500位元組(具體大小視公司情況而定),須要手動申請。
6、釋放之後的記憶體不可以再使用,如要使用需要再次申請。
7、如果同時申請兩塊記憶體,第一次申請成功,第二次申請失敗,一定要記得釋放掉第一次成功申請的記憶體。
執行個體代碼:
1 #define LEN 10 2 int main(int argc, const char * argv[]) { 3 int *p = NULL; 4 //申請一段空間,放10個int 5 p = (int*)malloc(10*sizeof(int)); 6 if(p==NULL){ 7 printf("malloc failed\n"); 8 return 0;//返回,不能進行下面的操作 9 }10 printf("before clear:");11 for(int i=0;i<LEN;i++){12 printf("%d, ",*(p+i));13 }14 printf("\n after clear:");15 memset(p,0x0,10*sizeof(int));//記憶體清016 for(int i=0;i<LEN;i++){17 printf("%d=0x%x, ",*(p+i),*(p+i));18 }19 /*資料處理*/20 free(p);//手動釋放21 p = NULL;//增強了保險,使p不可以用22 return 0;23 }
2)memset, 用於初始化記憶體空間, 結構 void *memset(void *, int, size_t);需要引用標頭檔<string.h>
參數1:表示記憶體的首地址
參數2:記憶體設定成多少[0,0xff]
參數3:設定多少位元組
3)free 用於釋放申請的記憶體,例如:free(p)。
4)calloc 用於申請記憶體; 不需要手動初始化,申請的記憶體空間可直接使用。
執行個體代碼:
1 int main() 2 { 3 int *p; 4 p = (int*)calloc(10, sizeof(int)); 5 if(p==NULL){ 6 printf("calloc failed\n"); 7 return 0; 8 }10 for(int i=0;i<10;i++){11 printf("%d ",*(p+i));12 }13 free(p);14 return 0;15 }
5)realloc , 重新調整/申請記憶體;結構void* realloc(void* ptr, unsigned newsize);可以擴大或者縮小。可能執行失敗擴大時,
可能分配不到,需要到新的地址申請,那麼資料被拷貝到新的位置,原來的記憶體將被free掉,realloc返回新記憶體的地址
例如:realloc(NULL, 200) 等價於 malloc(200); 表示新申請一個大小為200的記憶體;
realloc(ptr,0)等價於 free(ptr);表示釋放記憶體
執行個體代碼:
1 int main() 2 { 3 int *p; 4 p = (int*)malloc(10*4); 5 if(p == NULL) 6 return 0; 7 printf("fisrt alloc p=%p\n",p); 8 *p = 100; 9 //清零。。使用10 //擴大的記憶體比較大的時候,p可能會變11 p = (int*)realloc(p, 500);12 if(p == NULL) 13 return 0;14 printf("second alloc p=%p\n",p);15 printf("first int is %d\n",*p);16 free(p);17 return 0;18 }
6)memchr 用於在規定的記憶體範圍中尋找指定的字元,void *memchr(const void *src, int c, size_t size);
從s指標指向的記憶體中尋找 c , 找到了返回c在s中的地址,否則返回NULL
執行個體代碼:
1 int main() 2 { 3 char str[100] = "hello world"; 4 char ch = 'r'; 5 char *p; 6 p = (char*)memchr(str, ch, 3);//尋找範圍為前3個位元組 7 if(p == NULL) 8 printf("can not find the char.\n"); 9 else 10 printf("%s\n",p);11 return 0;12 }
7)memcpy 用於記憶體拷貝;void *memcpy(void *dst, const void *src, size_t size);
1、需要保證dst指向的記憶體空間足夠容納size個位元組
2、dst src指向的記憶體空間不能有重疊部分
執行個體代碼:
1 int main()2 {3 char str[100] = "hello world";4 char str2[20] = "zhongguo";5 memcpy(str+strlen(str), str2, strlen(str2)+1);//+1 多拷貝一個結束符6 printf("%s\n",str);7 }
8)memmove 用於記憶體移動;void *memmove(void *dst, const void *src, size_t len);dst src指向的空間可以重疊
執行個體代碼:
1 int main()2 {3 char str[100]="1234567890";4 // char *p = "hello";//p指向字串常量的首地址5 // memmove(str, p, 5);//相當於memcpy6 memmove(str, str+2, 5);7 printf("%s",str);//34567]678908 return 0;9 }
9)memcmp 用於比較字串大小int memcmp(const void *s1, const void *s2, size_t n);s1==s2 返回0;s1< s2,返回<0 ;
s1>s2,返回>0;返回的值=第一個不相等的字元ascii碼差值。
執行個體代碼:
1 int main() 2 { 3 char *p1 = "hello world"; 4 char *p2 = "helLo"; 5 int rst = memcmp(p1, p2, 6); 6 if(rst == 0) 7 printf("firt 6 chars equal\n"); 8 else 9 printf("not equal,%d\n",rst);10 return 0;11 }