The ninth day of IOS Learning (memory management), and the ninth day of ios

Source: Internet
Author: User

The ninth day of IOS Learning (memory management), and the ninth day of ios

Knowledge Point sorting for IOS Learning (C language)

I. Memory Management

1) malloc, used to apply for memory. The void * malloc (size_t) structure must reference the header file <stdlib. h>; apply for memory in the heap. size_t indicates the size of the applied space in bytes. If the application is successful, return the first address of the memory segment. If the application fails, return NULL; the Applied memory space needs to be manually initialized.

Note:

1. The application may fail, so you need to determine whether the return value is NULL.

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

3. The allocated memory is not initialized and needs to be cleared before use.

4. Manual release is required after use. If it is not released, memory leakage may occur.

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

6. The released memory cannot be used again. If you want to use the memory, apply again.

7. If you apply for two pieces of memory at the same time, the first application is successful, and the second application fails, remember to release the memory successfully applied for the first time.

Instance code:

1 # define LEN 10 2 int main (int argc, const char * argv []) {3 int * p = NULL; 4 // apply for a space, put 10 int 5 p = (int *) malloc (10 * sizeof (int); 6 if (p = NULL) {7 printf ("malloc failed \ n"); 8 return 0; // return, the following operation cannot be performed 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 )); // memory clearance 016 for (int I = 0; I <LEN; I ++) {17 printf ("% d = 0x % x ,", * (p + I), * (p + I); 18} 19/* Data Processing */20 free (p); // manually release 21 p = NULL; // enhanced insurance so that p cannot use 22 return 0; 23}

2) memset, used to initialize the memory space. The void * memset (void *, int, size_t) structure must reference the header file <string. h>

Parameter 1: indicates the first address of the memory.

Parameter 2: memory value [0, 0xff]

Parameter 3: How many bytes are set

3) free is used to release the applied memory, for example, free (p ).

4) calloc is used to apply for memory. Manual Initialization is not required, and the applied memory space can be directly used.

Instance code:

 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, re-adjust/apply for memory; Structure void * realloc (void * ptr, unsigned newsize); can be expanded or reduced. When the execution fails to expand,

The allocation may not be available. If you need to apply for a new address, the data will be copied to the new location, the original memory will be free, and realloc will return the address of the new memory.

For example, realloc (NULL, 200) is equivalent to malloc (200), indicating that a new memory with a size of 200 is applied;

Realloc (ptr, 0) is equivalent to free (ptr );

Instance code:

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 // reset .. When the 10 // memory size is relatively large, p may change to 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 is used to find the specified character in the specified memory range, void * memchr (const void * src, int c, size_t size );

Find c from the memory pointed to by s pointer and find the address of c returned in s; otherwise, return NULL.

Instance code:

1 int main () 2 {3 char str [100] = "hello world"; 4 char ch = 'R'; 5 char * p; 6 p = (char *) memchr (str, ch, 3); // the search range is the first 3 bytes. 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 is used for memory copy; void * memcpy (void * dst, const void * src, size_t size );

1. Ensure that the memory space pointed to by dst is sufficient to accommodate size bytes.

2. the memory space pointed to by dst src cannot overlap.

Instance code:

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 copy an ending character 6 printf ("% s \ n", str); 7}

8) memmove is used for memory movement. void * memmove (void * dst, const void * src, size_t len); the space pointed to by dst src can overlap.

Instance code:

1 int main () 2 {3 char str [100] = "1234567890"; 4 // char * p = "hello "; // p points to the first address of the String constant 5 // memmove (str, p, 5); // equivalent to memcpy6 memmove (str, str + 2, 5 ); 7 printf ("% 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.

Instance code:

 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 }

 

Related Article

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.