Handling of the Segmentation fault error in Linux programming

  
                

Some developers have programmed processes under Linux and found a Segmentation fault error when debugging with GDB. This error is mainly caused by accessing the wrong memory segment. It may be that there is no permission or the memory segment does not exist. How to deal with this problem?

follows:

This error is a result of how it? It turns out that a pointer of a char type is defined, and then the string is directly related to the pointer. For example:

char *c1;

for(i=0; i"n;i++)

{

*c1 = getchar();

c1++;

}

The code means this is the case, this is a problem that many developers will make. This pointer is dangerous, be cautious, just take it here, so assign pointers, we don't know where this pointer points, if the data written covers critical area data, it may have disastrous consequences, This is where you should visit. What is the solution? Tell this pointer to where it is. I use malloc to apply for a space for the string that the pointer will point to. This will indicate that the system allocates a safe space and will not divide the critical area of ​​the memory. For you. This way you can operate safely.

This example tells us that pointers can't be used. If the pointer points to the wrong memory, it will cause a Segmentation fault error during programming. You can also use the above method. If you point to other areas, it will lead to more serious. as a result of.

Copyright © Windows knowledge All Rights Reserved