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 }
新聞熱點
疑難解答