您找到段错误原因的方法是什么?

2022-01-12 00:00:00 c segmentation-fault c++

或者只是一般的调试,您喜欢如何查找代码中的错误.特别适用于 C/C++,但一般适用于所有语言.我一直在试图找出这个令人讨厌的段错误的原因,但我希望自己找到它,而不是在网上发布它.你对像我这样的学徒有什么建议吗?

Or just debugging in general, how do you like to go about finding bugs in code. Specifically for C/C++, but all languages in general. I've been trying to find the cause of this nasty segfault, but I'd like the challenge of finding it myself, not posting it online. Do you have any advice for a padawan such as myself?

推荐答案

尝试将你的代码推向糟糕的情况.

Try to push your code into bad situations.

如果您正在编写解析器,请将 BMP、JPG、随机文本扔给它,看看会发生什么.如果你正在编写一个 RPC 协议服务器,用大量并发请求使其超载,向其中写入垃圾,在不知不觉中断开客户端......

If you're writing a parser, throw BMPs, JPGs, random text at it, and see what happens. If you're writing a RPC protocol server, overload it with plenty of concurrent requests, write garbage into it, disconnect the client in the middle of nowhere...

一开始不要狡猾,尽可能地抛出,然后尝试欺骗你的代码.

Don't be subtle at first, throw whatever possible, but then try to trick your code.

相关文章