如何避免分段错误 [英] How to avoid segmentation faults

查看:96
本文介绍了如何避免分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在完成作业时,我总是遇到分段错误。实际上我使用调试器来解决它们但我想知道应该如何避免它们?



任何帮助都将受到赞赏。



我尝试过的事情:



我经常尝试但总是浪费一整天代码。 ..: - )

I always get segmentation faults while doing my assignments. Actually I do use debugger to solve them but I want to know how should we avoid them?

Any help will be appreciated.

What I have tried:

I usually try but always waste whole day for that single line of code... :-)

推荐答案

可能导致seg故障的典型初学者错误是:



使用未初始化的变量(特别是对于数组索引)。

始终初始化变量。



不检查函数返回值。

函数可能返回特殊值,如 NULL 指针或负整数以指示错误。或者返回值表示参数传回的值无效。

始终检查错误状态并在出错时中断执行。



循环到并包括数组的长度。

使用 length - 1 访问数组的最后一个元素。

循环结束条件必须<长度(不是< = )。



使用<的数组长度不正确code> sizeof 。

不要使用 sizeof 作为动态创建数组的指针(它只是指针大小)。

当确定固定数组的长度时,不要忘记在项大于一个字节时除以项目大小(使用 sizeof(数组)) / sizeof(array [0]))。



不改变文字数组大小。

在使用数组长度的所有位置使用 #define sizeof 运算符。



NULL处理不当终止字符串。

忘记为终止 NULL 字符。

忘记设置终止 NULL 字符。



可能还有更多。但以上是常见的,经常在CP上看到。



最后提示:

将编译器的警告级别设置为最大值。然后编译器会检测到一些上述问题,比如单元化变量。



使用静态代码分析器,如 Cppcheck - 用于静态C / C ++代码分析的工具 [ ^ ]。
Typical beginner errors that may lead to seg faults are:

Using uninitialised variables (especially for array indexes).
Always initialise variables.

Not checking function return values.
Functions might return special values like a NULL pointer or a negative integer to indicate an error. Or the return values indicates that values passed back by arguments are not valid.
Always check for error states and break execution upon errors.

Looping up to and including the length of an array.
The last element of an array is accessed with length - 1.
Loop end conditions must be < length (not <=).

Improper length for arrays using sizeof.
Don't use sizeof for pointers of dynamically created arrays (it is just the pointer size).
Don't forget to divide by the item size when the items are larger than a byte when determining the length of fixed arrays (use sizeof(array) / sizeof(array[0])).

Changing literal array sizes not at all places.
Use a #define or the sizeof operator at all places where the array length is used.

Improper handling of NULL terminated strings.
Forgetting to allocate space for the terminating NULL character.
Forgetting to set the terminating NULL character.

And probably more. But the above are common and are seen here at CP quite often.

Final tips:
Set the warning level of the compiler to maximum. Some of the above problems like unitialised variables are then detected by the compiler.

Use a static code analyser like Cppcheck - A tool for static C/C++ code analysis[^].


仔细编程是避免它们的方法。像任何 C / C ++ 程序员一样,你应该掌握动态内存分配,指针等。
Careful programming is the way to avoid them. Like any C/C++ programmer, you should master dynamic memory allocation, pointers, and the like.


引用:

分段错误



这通常发生在你的程序试图访问它不拥有的地方时。

聪明地使用调试器将帮助您找到问题所在。

如果没有看到您的代码,我们无法帮助您。


This happen typically when your program try to access a place it don't own.
A clever use of debugger will help you to find where is the problem.
We can't help you mire without seeing your code.


这篇关于如何避免分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆