Linux Error: Segmentation fault.

Source: Internet
Author: User

Problem encountered: The program is ready to execute fclose (FP) After reading the file, and the following error occurred:
Program received signal SIGSEGV, segmentation fault.

Solution: Daoteng for a long time, the results found to be a small & in trouble.
original put read (f, Buff, nfilesize)
written in read (F, &buff, nfilesize), more than A &!

Experience Summary:

1. About segment Errors (segmentation fault):
the so-called segment error means that the memory accessed exceeds the memory space given to the program by the system, usually this value is saved by GDTR, which is a 48-bit register, where 32 bits are saved by the GDT that it points to, and the last 13 bits are stored accordingly to the GDT,  The last 3 bits include whether the program is in memory and the program's operating level in the CPU, and the GDT that points to is a table with a 64-bit unit, which in this table It holds the code snippet that the program runs and the starting address of the data segment, as well as information about the corresponding segment limit and page Exchange, the program run level, and the granularity of the memory. Once a program has crossed access, the CPU will have the corresponding exception protection, so segmentation fault appeared.

2. Causes

In programming, the following types of practices tend to lead to segment errors, which are basically caused by using pointers incorrectly:
1) access to the system data area, especially to the system protected memory address write data, the most common is to give a pointer to 0 address
2) memory out of bounds (array out of bounds, variable type inconsistent, etc.) access to areas of memory that do not belong to you

3) Other

Therefore, be aware that:
① define the pointer and remember to initialize it, remember to determine if it is null when used
Whether the ② is initialized when using an array, whether the array subscript is out of bounds, whether the array element exists, etc.
③ whether the variable format control is reasonable when the variable is processed

The specific solution can be referred to as follows:

http://tech.diannaodian.com/dw/lin/2012/0604/180621.htmlhttp://blog.csdn.net/love_gaohz/article/details/6597857

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.