随笔- 509  文章- 0  评论- 151  阅读- 22万 

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   zhuli19901106  阅读(185)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示