During programming yesterday, segmentation fault was encountered. I found it only when debugging with GDB, by the way. I am used to graphic interface debugging tools, but I am not used to using command lines. I asked Google about this error. I searched a large article and it seems to be a very common question.
Simply put, this error is caused by illegal access to the memory in the system, such as some key memory areas in the system. So how did my mistake result from it? It turns out that I am defining a char pointer, and then directly perform string-related operations on this pointer. For example:
Char * C1; for (I = 0; I <n; I ++) {* C1 = getchar (); C1 ++ ;}
The code is probably like this. Later I thought about it before I found that I was tired again. This pointer is dangerous. You need to be cautious. For example, if you assign a value to the pointer like this, who knows where it points, if the written data overwrites the data in key areas, it may have disastrous consequences. This is where the access should not be accessed. What is the solution? Tell this pointer to the point where it is located. I use malloc to apply for a space for the string to be pointed to by the pointer. This will indicate that the system will allocate a safe space, you will not be allocated key areas of the memory. In this way, you can perform security operations.
For pointers, you cannot take it lightly.