Write this articleArticleInstead of having a deep understanding of the pointer issue, I hope to improve myself through sharing.
Before talking about pointers, let's talk about null. In this case, we need to talk about the definition # define null (void *) 0. From the definition, we can clearly know that the pointer pointing to null actually points the pointer's initial point to the address 0, and this address is usually used as our function to determine whether the pointer is null, so is the 0 address really empty or does it not exist? If it is not empty, why can this address be used as a special value for determining whether to allocate memory? In fact, the zero address exists, but this address has been used in a special way in the system design, and the corresponding protection is adopted at the beginning. We cannot modify it, of course, it cannot be assigned to our applications.ProgramFor data operations, and other memory addresses can be modified by the system or by us under certain system conditions. This is why we can take it as a criterion. In view of this, in order to prevent operations on the wild pointer, we can immediately set it to null after initialization and usage of a pointer and take a judgment on it.
Next, we can officially discuss the pointer issue. We know that the memory space occupied by them is the same regardless of what we define, that is, the memory space used to store a pointer address value space. The reason for the existence of multiple data type pointers is to limit the location and the size of the accessed space through pointer operations. This is why the * pointer must be defined and the Data Type pointed to by it must be well known. This is very important for pointers that do not directly use functions such as malloc () to allocate memory space. Of course, we can define all pointers to memory allocated with malloc () as void *. Because we have a way to access the memory space of a data type with a specified length.
Next, let's talk about the memory management problem that the local variable points back to and serves as the return value. We can use malloc () and free () to manage the memory. We need to return a pointer for this pair, but it is a feasible solution to point to a local variable that causes memory problems in the application (because I am not sure whether this is the best, so I can only determine whether it is an optional solution ).