1.malloc(1) malloc allocation function: The size of the request space (in bytes) needs to be given(2) The return value is a first address, received with the pointer(3) Calculate element size using sizeof(4) Use the MALLOC function to request memory space, need to release after use, otherwise it will cause memory leak(5) Release function free needs pointers to allocated memory(6) Basic form: void *malloc (unsigned int size);(7) Allocate a specified size of memory space, but will not clear the allocated space 0(8) free (pointer);//release heap space, mark Delete, unclear content(9) Example:①malloc and one-dimensional arrays"1" Go * plus square brackets, pointers, array two transforms, no matter how many dimensional arrays, a few * can"2" randomly generates 10 integers, saved to the heap areaint *parr = malloc (sizeof (int) * 10);//opening 10 integral spaces in the heap areafor (int i = 0; i <; i + +) {Parr[i] = arc4random ()% (100-10 + 1) + 10;//generates a random number of 10 "10,100"
}for (int i = 0; i <; i + +) {printf ("%d", parr[i]);//Output
}printf ("\ n");//NewLinefree (parr);//space to release applications②malloc and two-dimensional arrays"1" assigns a two-dimensional array of 2 rows and three columns"2" int (*PA) [3] = malloc (sizeof (int) *2*3);Int (*PA) [3] = malloc (sizeof (int) * 2 * 3);//Open an integer space of 2 rows and 3 columnspa[0][0] = 1;//Assignmentpa[0][1] =;③malloc and Strings" 1" Saves the string "Hello,lanou" to the heap areaprogramme One:char *pst = malloc (sizeof (char) * 12);//Open 12 char spaces, note the end of the stringstrcpy (PST, "Hello,lanou");//Copy Stringprintf ("%s\n", PST);//Outputfree (PST);//Release MemoryScenario Two:char a[] = "Hello,lanou";//String that copies the constant area in the stack areachar *ps = malloc (sizeof (a));//space to open the string sizestrcpy (ps,a);//Copy the string from the stack to the heap areaFree (PS);//Release Memory④malloc and string arrays"1" char (*P) [255] = malloc (5 * 255);//Open 5 strings, each string 255 bytes of space" 2" strcpy (P[0], "iPhone");//Copy the string to the first array of strings⑤malloc and structural bodytypedef struct PERSON{//creates a struct with 2 elementsChar name[20];int age;
}person;programme One:int main () {Person *pp = malloc (sizeof);//open up a space for the structure sizestrcpy (Pp->name, "six Dolls");//String copy, Assignmentprintf ("%s\n", pp->name);//Outputpp->age = 34;//integral type assignmentprintf ("%d\n", pp->age);//Outputfree (PP);//Release Memory}Scenario Two:features that can be directly assigned according to the structurePerson p = {"Bei ye", 20};//first assigns initial value to struct bodyPerson *pi = malloc (sizeof);//open up a space for the structure size*pi = p;//Copies all the values of the structure of the stack area directly to the PI in the heap area⑥malloc and struct arrays"1" pointer with arrows, structure array with points"2" 3 elements of the structure of a body arrayprogramme One:person *pt = malloc (sizeof (person) * 3);//Open 3 space for the structure sizestrcpy (Pt[0].name, "Lanou");//((pt + 0)->name), string copypt[0].age = 10;//assigns a value to an element of the first struct of the struct arrayprintf ("%s\n", pt[0].name);//Outputfree (PT);//Release MemoryScenario Two:Person Pt[3] = {{"Erdanzi", 23},{"Bee", 20},{"Liuwa", [+]};Person *pp = malloc (sizeof (person) * 3);for (int i = 0; i < 3; i + +) {Pp[i] = pt[i];//between structures can be directly assigned
}for (int i = 0; i < 3; i + +) {printf ("%s%d\n", pp[i].name,pp[i].age);
}free (PP);//Release Memory2. Other memory allocation functions①calloc"1" void *calloc (unsigned n, unsigned size);"2" Using free release"3" allocates n sizes of size space"4" differs from malloc in that the memory space requested by Calloc is initialized to 0"5" is not recommended, it is easy to cause access to the cross. C language does not have cross-border protection, programmers need to check their own boundaries②realloc"1" void *realloc (void *, unsigned newSize)"2" Using free release"3" is reassigned at a given address and a given size"4" from small to large, data will not be lost"5" from big to small, data may be lostExample://Original Allocated spaceint *p = malloc (+);//re-allocated spacep = realloc (p,150);3. Memory manipulation Functions①meset"1" void *memset (void *s, int c, size_t n);"2" not only for the heap area, but also for the stack areastarting with "3", all bytes of length n are assigned a value of C"4" is typically used to clear structure or array data②memcpy"1" void *memcpy (void *dest, const void *source, size_t n);"2" copies n bytes from source to Dest"3" not only for the heap area, but also for the stack area③memcmp"1" int memcmp (const void *BUF1, const void *BUF2, unsigned int count)"2" Memory comparison, comparison results are divided into >0, <0, =0"3" can be used for both heap memory and stack memory
Second, dynamic memory allocation and use