C language-memory Partition
I. Stack zone, heap zone, static zone (Global zone), constant zone, and code zone:
1. STACK: the storage space is opened up by the system based on the number of bytes occupied by the data type in this region. after use, the storage space is released by the system.
(System allocation, system release)
2. heap, manual allocation, and manual release.
3. static and static: The program will not be released until the end of the program running.
4. constants occupy memory and are read-only and cannot be modified.
5. After Code and all statements are compiled, CPU commands are generated and stored in the code area.
Ii. Use of malloc, calloc, and realloc functions:
1,void * mallloc ( unsigned size ) ;
2,void * calloc ( unsigned int n , unsigned size ) ;
N size space (n * size). After allocation, the entire space is cleared.
3,void * realloc ( void *p , unsigned newSize ) ;
Reallocation Based on the given address and the given size
Iii. Memory operation functions:
1,void * memset ( void *s , int c , size_t n ) ;
Copy From s to memory to dest,
2,void * memcpy ( void * dest , const void * source , size_t n ) ;
Copy from source to memory to dest and n Bytes
3,int memcmp( const void * buf1 , const void * buf2 , unsigned int count ) ;
Compare whether buf 1 and buf 2 point to the same memory, count bytes
Greater than zero, equal to zero, Less Than Zero
Example:
/// Main. m // C_Project_10 /// Created by on 15/3/25. // Copyright (c) 2015. All rights reserved. // # import
Void word () {char * words [3] = {0}; int I = 0; while (I <3) {words [I] = (char *) malloc (INT32_MAX); I ++;} printf ("Enter three strings: \ n"); for (int I = 0; I <3; I ++) scanf ("% s", words [I]); for (int I = 0; I <3; I ++) {printf ("address: % p-> content: % s \ n ", words [I], words [I]); free (words [I]); // who should release? Words [I] = NULL ;}} void callocAndRealloc () {// The calloc function is used to allocate n size bytes, int * p = calloc (4, sizeof (int); for (int I = 0; I <4; I ++) printf ("% d \ t", * (p + I); printf ("\ n"); // The realloc function has two functions: // 1. determine whether there is sufficient available space in the future based on the first address of the given existing space. If yes, add newSize in sequence -- the number of bytes of the original size; // 2. If no available space exists, search for newSize space in other spaces in the heap area, copy the data in the original space, and release the memory of the original space; // 3. This is also the address returned by realloc, which is sometimes the same as the input address, sometimes different from the original Because. Int * q = realloc (p, 10000); printf ("% p \ n", p); printf ("% p \ n", q ); // The memset function initializes the specified heap memory space. The parameter list is (void * p, int c, int size ); it indicates that the size of the space pointed to by p is initialized to c. // Initialize each byte to an integer c. Q = memset (q, 1, 2000); for (int I = 0; I <500; I ++) printf ("memory initialization: % d ", * (q + I); free (p); p = NULL; free (q); q = NULL;} void memsetAndMemcpy () {int * p = malloc (sizeof (int) * 4); for (int I = 0; I <4; I ++) {* (p + I) = arc4random () % 21 + 10; printf ("% d \ t", * (p + I);} printf ("\ n "); int * q = malloc (sizeof (int) * 4 ); // copy the specified n bytes in the heap memory space pointed by the second pointer in the function parameter list to the corresponding location of the memory space pointed by the first pointer. Current memory copy process. Memcpy (q, p, sizeof (int) * 4); for (int I = 0; I <4; I ++) {printf ("% d \ t ", * (q + I);} * q = 9; // The memcmp function specifies the number of bytes in the heap memory space to which the two pointers are directed. The difference is calculated by byte, if the final result is 0, the content of the space to be compared is the same; otherwise, the content is different. Int result = memcmp (p, q, sizeof (int) * 4); printf ("\ n two strings comparison result: result = % d \ n", result ); free (p); p = NULL; free (q); q = NULL;} void Homework () {int * p = (int *) malloc (sizeof (int) * 3); int * q = (int *) calloc (3, sizeof (int); p = memset (p, 0, 12); for (int I = 0; I <3; I ++) {* (p + I) = arc4random () % 3 + 1; printf ("p = % d \ n ", * (p + I); * (q + I) = arc4random () % 3 + 1; printf ("Q = % d \ n", * (q + I);} if (0 = memcmp (p, q, sizeof (int) * 3 )) {printf ("GOOD! \ N ");} else {printf (" Falied! \ N ") ;}free (p); p = NULL; free (q); q = NULL;} int main (int argc, const char * argv []) {// word (); // callocAndRealloc (); // memsetAndMemcpy (); Homework (); // 1. The heap memory allocation function malloc (), the Return Value Type of this function is void *, indicating any pointer type (generic). Its parameter is the size of the space to be opened, in bytes. // Int * p = malloc (sizeof (int) * 4); // printf ("% p \ n", p ); // allocated heap address // for (int I = 0; I <4; I ++) // {// * (p + I) = arc4random () % 21 + 10; // printf ("address: % p-> content: % d \ n", p + I, * (p + I )); ///} // * p = 3; // printf ("% d \ n", * p); // int a = 3; // printf ("% p \ n", & a); // after comparing with the heap address, it is found that the address length is greater than that in the heap area, //// free (p); /// 2. The storage area allocated by the heap memory allocation function. after use, call Mark delete function free () to release the function. Mark deletion: indicates that the currently used storage area can be re-allocated to other required data, but the data stored by the storage area will always exist before the re-allocation. // P = NULL; // to prevent the occurrence of a wild pointer, after the tag is deleted, it indicates that the space is no longer used and the pointer must be pointed to NULL. /// Printf ("% p \ n", p); // char str [] = "abcdef123456ABCDEF789"; // int size = 0; //// for (int I = 0; I <strlen (str); I ++) // {// if (str [I]> = '0' & str [I] <= '9') // {// size ++; //} //} // char * number = (char *) malloc (sizeof (char) * size); // for (int I = 0; I <strlen (str); I ++) // {// if (str [I]> = '0' & str [I] <= '9 ') // {// * number = str [I]; // number ++; //} // print F ("% s \ n", number-size); // free (number-size); // if (number! = NULL) // number = NULL; return 0 ;}