IOS Stage Learning Nineth Day notes (memory management)

Source: Internet
Author: User

iOS learning (c language) knowledge point finishing

First, memory management

1) malloc, used to apply for memory; struct void *malloc (size_t), need to reference header file <stdlib.h>; request memory inside Heap, size_t, indicates the size of the application space, Unit is byte, if the request succeeds, returns the first address of this memory, the request fails, returns null; Manual initialization required

Note the point:

1, may apply for failure, so you need to determine whether the return is null.

2. The requested memory needs to be cast to the specified data type, for example: (int*) malloc (10*sizeof (int))

3. The allocated memory is uninitialized and needs to be cleaned before use.

4. It needs to be released manually, if not released, it will cause a memory leak.

5. When to use: >500 bytes (depending on the size of the company), you need to apply manually.

6, after the release of memory can not be used again, if you want to use the need to apply again.

7, if you apply for two pieces of memory at the same time, the first successful application, the second application failed, you must remember to release the first successful application of memory.

Instance code:

1 #defineLEN 102 intMainintargcConst Char*argv[]) {3     int*p =NULL;4     //apply for a space, put 10 int5p = (int*)malloc(Ten*sizeof(int));6     if(p==NULL) {7printf"malloc failed\n");8         return 0;//returned, unable to do the following9     }Tenprintf"before clear:"); One      for(intI=0; i<len;i++){ Aprintf"%d,", * (p+i)); -     } -printf"\ n after clear:"); theMemset (P,0x0,Ten*sizeof(int));//Memory Clear 0 -      for(intI=0; i<len;i++){ -printf"%d=0x%x,", * (P+i), * (p+i)); -   } +   /*Data Processing*/ -      Free(p);//Manual Release +p = NULL;//Enhanced insurance so that p cannot be used A     return 0; at}

2) memset, used to initialize memory space, struct void *memset (void *, int, size_t); Reference header file required <string.h>

Parameter 1: The first address of memory

Parameter 2: How much memory is set [0,0xff]

Parameter 3: How many bytes are set

3) Free is used to release the requested memory, for example: "P".

4) Calloc used for memory application; No manual initialization is required and the requested memory space can be used directly.

Instance code:

1  intMain ()2 {3     int*p;4p = (int*)calloc(Ten,sizeof(int));5     if(p==NULL) {6printf"calloc failed\n");7         return 0;8     }Ten      for(intI=0;i<Ten; i++){ Oneprintf"%d", * (p+i)); A     } -      Free(p); -     return 0; the}

5) ReAlloc, re-adjust/apply for memory, structure void* realloc (void* ptr, unsigned newsize), can be enlarged or reduced. When the failure to perform is expanded,

may not be allocated to the new address request, then the data is copied to the new location, the original memory will be free, realloc return the new memory address

For example: ReAlloc (NULL, 200) is equivalent to malloc (200); Represents a new request for a size of 200 memory;

ReAlloc (ptr,0) is equivalent to free (PTR), which means freeing memory

Instance code:

1 intMain ()2 {3     int*p;4p = (int*)malloc(Ten*4);5     if(p = =NULL)6         return 0;7printf"fisrt alloc p=%p\n", p);8*p = -;9     //Clear Zero. UseTen     //when the enlarged memory is large, p may change Onep = (int*)realloc(P, -); A     if(p = =NULL) -         return 0; -printf"Second Alloc p=%p\n", p); theprintf"First int is%d\n",*p); -      Free(p); -     return 0; -}

6) MEMCHR is used to find the specified character in the defined memory range, void *MEMCHR (const void *SRC, int c, size_t size);

Find C in the memory pointed to by S pointer, find the address that returns C in S, otherwise return null

Instance code:

1 intMain ()2 {3     Charstr[ -] ="Hello World";4     CharCH ='R';5     Char*p;6p = (Char*) MEMCHR (str, CH,3);//The lookup range is the first 3 bytes7     if(p = =NULL)8printf"can not find the char.\n");9      Else Tenprintf"%s\n", p); One     return 0; A}

7) memcpy for memory copy; void *memcpy (void *dst, const void *SRC, size_t size);

1. Need to ensure that DST points to enough memory space to accommodate size bytes

2. The memory space that the DST src points to cannot have overlapping parts

Instance code:

1 intMain ()2 {3     Charstr[ -] ="Hello World";4     Charstr2[ -] ="Zhongguo";5memcpy (Str+strlen (str), STR2, strlen (str2) +1);//+ 1 multiple copies one terminator6printf"%s\n", str);7}

8) Memmove for memory movement, void *memmove (void *dst, const void *SRC, size_t len);d St src points to a space that can overlap

Instance code:

1 intMain ()2 {3     Charstr[ -]="1234567890";4 //char *p = "Hello";//P point to the first address of the string constant5 //memmove (str, p, 5);//equivalent to memcpy6Memmove (str, str+2,5);7printf"%s", str);//34567]678908     return 0;9}

9) memcmp is used to compare the string size int memcmp (const void *S1, const void *S2, size_t n); S1==s2 returns 0;s1< s2, returns <0;

S1>S2, return >0; return value = The first unequal character ASCII code difference value.

Instance code:

1 intMain ()2 {3     Char*P1 ="Hello World";4     Char*P2 ="HelLo";5     intrst = memcmp (P1, p2,6);6     if(rst = =0)7printf"firt 6 chars equal\n");8     Else9printf"Not equal,%d\n", RST);Ten     return 0; One}

IOS Stage Learning Nineth Day notes (memory management)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.