Segmentation fault in Linux Programming

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.