在GDB调试过程中遇到 Segmentation Violations/Faults 的解决方案

Troubleshooting Segmentation Violations/Faults

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.

Common causes of this problem:

触发问题的原因:

  • 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.

  • 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.

  • 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.

  • 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.

  • 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.

      防范措施:

      Minimizing the use of pointer variables.(减少使用指针变量)

    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.

      Troubleshooting the problem:

    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.

    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.

你可能感兴趣的:(在GDB调试过程中遇到 Segmentation Violations/Faults 的解决方案)