A common run-time error for C programs by beginners is a "segmentation violation" or "segmentation fault." When you run your program and the system reports a "segmentation violation," it means your program has attempted to access an area of memory that it is not allowed to access. In other words, it attempted to stomp on memory ground that is beyond the limits that the operating system (e.g., Unix) has allocated for your program. (錯誤緣由:試圖進入一個不容許進入的內存地址,換句話說,超出了系統分配給你的程序的內存。) 數組
Any time your program gives a "segmentation violation" or "segmentation fault" error, review this document for tips on correcting the error. app
觸發問題的緣由: less
Improper format control string in printf or scanf statements:(在printf或者scanf語句中不恰當的控制字符格式)
Make sure the format control string has the same number of conversion specifiers (%'s) as the printf or scanf has arguments to be printed or read, respectively, and that the specifiers match the type of variable to be printed or read. This also applies to fprintf and fscanf. ide
Forgetting to use "&" on the arguments to scanf:(在scanf參數中忘記使用「&」)
Function scanf takes as arguments the format control string and the addresses of variables in which it will place the data that it reads in. The "&" (address of) operator is used to supply the address of a variable. It is common to forget to use "&" with each variable in a scanf call. Omitting the "&" can cause a segmentation violation. ui
Accessing beyond the bounds of an array:(訪問超過數組的長度)
Make sure that you have not violated the bounds of any array you are using; i.e., you have not subscripted the array with a value less than the index of its lowest element or greater than the index of its highest element. this
Failure to initialize a pointer before accessing it:(訪問時未初始化指針)
A pointer variable must be assigned a valid address (i.e., appear on the left-hand-side of an assignment) before being accessed (i.e., appearing on the right-hand-side of an assignment). Make sure that you have initialized all pointers to point to a valid area of memory. Proper pointer initialization can be done several ways. Examples are listed below. spa
Incorrect use of the "&" (address of) and "*" (dereferencing) operators:【不正確地使用「 & 」(地址)和「 * 」(取值)運算符】
Make sure you understand how these operators work. Know when they should be applied and when not to apply them. As mentioned above, it is common to forget to use "&" with each variable in a scanf call. Remember, scanf requires the address of the variables it is reading in. Especially, know when "&" and "*" are absolutely necessary and when it is better to avoid using them. debug
Also, many times a function requires that an address (corresponding to a parameter of pointer type) be sent to it as an argument (as is true of many of the Numerical Recipes in C functions). The standard function scanf is an example of such a function.
指針
Check EVERY place in your program that uses pointers(指針), subscripts an array(數組腳註), or uses the address operator (&) and the dereferencing operator (*)【兩種運算符的使用】. Each is a candidate for being the cause of a segmentation violation. Make sure that you understand the use of pointers and the related operators. orm
If the program uses many pointers and has many occurrences of & and *, then add some printf statements to pinpoint the place at which the program causes the error and investigate the pointers and variables involved in that statement.
Remember that printf statements for debugging purposes should have a new-line character (\n) at the end of their format control strings to force flushing of the print buffer.