2014-04-24 23:15

题目:你有一段程序,运行了十次每次都在不同的地方崩掉了。已知这段程序只用了标准C或C++库函数,请问有什么思路来找出问题所在。

解法:1. 时间戳每次都不同。2. 随机变量每次都不同。3. 局部变量的初值,每次可能不同,不过就算没初始化,很多时候那个无效值其实也是固定的,和编译器操作系统之类的相关。4. 外部依赖引起的问题,这点不太容易说清楚,简而言之:那是隔壁组的问题。5. 内存泄露,读到了非法区域,数据的确是不确定的。

代码:

 1 // 12.2 A program crashes when it is run. After running it on a same machine for 10 times, it crashes every time on a different spot.
 2 // It is single-threaded, and uses only C-standard libraries.
 3 // Answer:
 4 //    One different thing when the program is run every time, is the timestamp.
 5 //    So you might check the code where functions about time is called, or timestamp is used.
 6 //    To debug a bugged system, use logging strategy to aid you.
 7 //    Two tips for debugging:
 8 //        1. start printing log at both ends, every time you crashes, shrink the range of debugging by half. Use the ideas of binary search for debug.
 9 //        2. print every suspicious variable if necessary.
10 int main()
11 {
12     return 0;
13 }

 

 posted on 2014-04-24 23:26  zhuli19901106  阅读(184)  评论(0编辑  收藏  举报