iOS training------My C language notes, look forward to communicating with you!
In the C language about memory is a very important point of knowledge, so today I will start from the C language memory allocation for everyone to analyze the C language and iOS development of very important knowledge.
Introduction to the 1:malloc function
Open memory space in C: malloc function
In the C language, the malloc principle is roughly the same:
- The real body of the malloc function now, it has a so-called idle list that connects the available memory blocks to a long listing. When you call the malloc function, it looks for a block of memory along the join table that is large enough to satisfy the user's request. The memory block is then split in two (the size of the chunk is equal to the size of the user request, and the other is the remaining bytes). Next, pass the memory allocated to the user to the user and return the rest of the block (if any) to the connection table. When the free function is called, it connects the memory blocks freed by the user to the idle chain. In the end, the idle chain will be cut into a lot of small memory fragments, if the user requests a large memory fragment, then the idle chain may not be able to meet the user requirements of the fragment. The malloc function then requests a delay and begins rummaging through the memory fragments on the idle chain, collating them, and merging the adjacent small free blocks into larger chunks of memory. If a block of memory is not available, the malloc function returns a null pointer, so it is important to determine the return value when invoking the malloc dynamic request memory block.
#include <stdio.h>
Use malloc to add header files
#include <stdlib.h>
- The declaration of the malloc function is like this.
- void *malloc (size_t size);//size_t unsigned
- Void is a paradigm: A pointer variable (the address of the first byte) that can be assigned to any type
The use of 2:malloc function and its points of attention
int main (void)
{
Char *p = malloc (1000);//pass parameter, here is 1000 bytes (this will be back in a space called the heap segment, the main function is open in the stack)
A stack is a space of two ends that have a distance in memory
P[0];//+1 is added 1 bytes to get the length (here 0 refers to the first one)
Through P[x] can use every byte of his space
- Another space, 1000, so it should be the same as above.
- Stack is an advanced post-out data structure
- int *q = malloc (1000);
- Using Q We can access the heap space in four bytes
- The space itself has no type
return 0;
}
3: Handling of memory errors in C language
#include <stdio.h>
#include <stdlib.h>
The chance of the heap space opening may fail, returning 0 means failure: This time we use Exit (-1); End Current program (process)
int main (void)
{
Char *p = malloc (1000);
if (p = = NULL) {//Error and Terminate program
Perror ("malloc");
Exit (-1);
}
P[0] = ' A '//using heap space
Free (p)//This is the release of heap space, this space can be used by other programs for a long time, so it becomes unsafe. And you must use the free function to release
return 0;
}
/**
Note
The stack space is static memory allocation, the size of the compilation is determined, and the memory space is automatically released
Heap space is dynamic memory allocation, when the size of the run is determined and needs to be released manually
*/
Description of the stack:
- Stack-the compiler automatically allocates releases, stores the function's parameter values, the values of local variables, and so on. It operates in a manner similar to a stack in a data structure.
- Heap area-typically released by programmers, if the programmer does not release, the program may end up being recycled by the OS. Note that it is not the same as the heap in the data structure, but the distribution is similar to the linked list
In addition, there are some functions in the C language that we should pay attention to:
- ReAlloc (re-allocating memory)
- Calloc (initialized to 0)
- Alloca (Request memory on stack, auto release)
If you also heap in C language memory allocation does not understand or heap stack does not understand so, please take a careful look at this picture, and then combined with the above introduction, you will be very clear.
"Good Programmer's note sharing"--Dynamic memory analysis